You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
... the validation will be called, even if the attribute value is null/undefined, forcing a check to be added to the validation. This seems counter-intuitive and makes the schema validation settings more complex.
As a side note, there seems to be a documentation mismatch:
Expected behavior
If an attribute has the required property set to false, validation should not be called when the value is null or undefined.
The text was updated successfully, but these errors were encountered:
solaris007
changed the title
validation should not be called when attribute is not required and null/undefinedvalidate should not be called when attribute is not required and null/undefined
Nov 26, 2024
The validate fn is ultimately the gatekeeper of the column and is there to ensure that any value set to that column is allowed. The only time the function would called without the user supplying a value is during a create, put, or upsert operation, where all values are validated (even undefined ones) to ensure the state of each column is valid.
If you would like that value to never arrive undefined but also not have it be required, you could use a default:
someAttribute: {type: 'any',// you could set this `true` here when `default` is used; having a default will change the typing of this attribute to optionalrequired: false,// default: () => ({ someInitialKey: "someInitialValue" }) <-- can also be a functiondefault: {someInitialKey: "someInitialValue"},// validate will receive the default, and only be invoked with user-applied values for the rest of the item's life cyclevalidate: (value)=>isNonEmptyObject(value),}
Describe the bug
When an attribute is declared as not required and specifies a
validate
property, e.g.:... the validation will be called, even if the attribute value is null/undefined, forcing a check to be added to the validation. This seems counter-intuitive and makes the schema validation settings more complex.
As a side note, there seems to be a documentation mismatch:
validate
property: https://electrodb.dev/en/modeling/attributes/#validatevalidation
property (which is never called, verified with debugger): https://electrodb.dev/en/modeling/attributes/#attribute-validationElectroDB Version
3.0.1
Expected behavior
If an attribute has the
required
property set tofalse
, validation should not be called when the value isnull
orundefined
.The text was updated successfully, but these errors were encountered: