Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required property to schema definitions #173

Open
bholmesdev opened this issue Aug 22, 2024 · 1 comment
Open

Add required property to schema definitions #173

bholmesdev opened this issue Aug 22, 2024 · 1 comment
Assignees
Labels
thinking about how to do it Not forgotten, but need to sleep on it

Comments

@bholmesdev
Copy link

Is your feature request related to a problem? Please describe.

I've been using schemas for typed queries on my store. The tooling is excellent, but I was surprised to find "not null" checks are required for all fields. I understand this is a design decision to keep TinyBase flexible. However, I expected a required property to be supported on schemas to assert values are not null when adding entries.

store.setTablesSchema({
  pets: {
    species: {type: 'string'},
    legs: {type: 'number', default: 4},
    sold: {type: 'boolean', default: false},
  },
}).addRow('pets', {
  // all values can be undefined here
});

Describe the solution you'd like

I'd like to see a required property on schema definitions to enforce defined fields. I'd expect this to be a boolean value alongside type and default:

store.setTablesSchema({
  pets: {
    species: {
      type: 'string',
+     required: true,
    },
    legs: {type: 'number', default: 4},
    sold: {type: 'boolean', default: false},
  },
});

Describe alternatives you've considered
I've asserted that all values are not-null when adding or updating entries. This gets quite noisy when using TypeScript.

Additional context
I recall hearing required was supported when listening to your interview on the Local-first podcast (great stuff by the way!). However, I haven't been able to find this in the documentation or by attempting to add required in my code.

@jamesgpearce
Copy link
Contributor

Hey @bholmesdev! My original intention was that the presence of default implicitly also means required - in other words, a value always has to be there, and if you don't supply it, then the required value will be used.

Of the top of my head, if you provide an entirely empty object, I don't think it will constitute a whole default row for you. (If not maybe it should).

It's also possible I messed up the schema-inferred typing though. Let me noodle on this for a bit.

@jamesgpearce jamesgpearce self-assigned this Sep 5, 2024
@jamesgpearce jamesgpearce added the thinking about how to do it Not forgotten, but need to sleep on it label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
thinking about how to do it Not forgotten, but need to sleep on it
Projects
None yet
Development

No branches or pull requests

2 participants