-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Stricter field type, and nullable by default #95
Comments
I think that making field types stricter is a great idea, I think it's more in-line with the spirit of typescript. If one wishes for I don't have a strong opinion on whether to make attributes nullable by default, but I think that is orthogonal to the behavior of the default value. As per the spec [emphasis mine]:
I contend that class User extends Model {
static entity = 'users'
@Num(50)
@NonNullable()
age!: number
@Str('John Doe')
name!: string | null
} should have the following behavior: const r = store.$repo(User);
// saves { age: 50, name: 'John Doe' }
r.save({});
// saves { age: 50, name: null }
r.save({ name: null });
// throws Error - age is non-nullable
r.save({ age: null }); I'm also curious about specification of behavior for fields without a default. I don't know what the correct/current behavior is, but for example: class Post extends Model {
@Str()
@NonNullable()
body!: string
@Str()
title: string | null
}
const r = store.$repo(Model);
// throws Error - body is non-nullable and has no default
r.save({ title: "B" });
// what about a nullable field with no default?
// throws Error - makes the most sense to me.
// { title: null, body: "test" } - rather this be expressed as `Str(null)`
// { title: undefined, body: "test" }
// { body: "test" }
r.save({ body: "test" }); it's possible that this diverges too much from the current behavior, but this is how I think it should behave based on my understanding of the meaning of |
Thanks for the feedback!
Yes, I think this is how it should work if make |
This is still just an idea, so looking for feedback.
What do you think about making model attributes more strict? Currently, we have several fields with primitive types, such as
string
,number
. etc. At the moment, these fields will cast values rather than restrict them. So passingtrue
tostring
field will make it'true'
.The idea is to make them fail instead of casting.
In addition to this, how about if we make all the fields nullable by default. Because in the frontend, usually many fields are going to be nullable.
So instead of having
nullable
option, we'll addNonNullable
decorator (if this is possible).The default value should behave the same,
The text was updated successfully, but these errors were encountered: