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

validate should not be called when attribute is not required and null/undefined #449

Open
solaris007 opened this issue Nov 26, 2024 · 2 comments

Comments

@solaris007
Copy link

Describe the bug
When an attribute is declared as not required and specifies a validate property, e.g.:

    someAttribute: {
      type: 'any',
      required: false,
      validate: (value) => !value || isNonEmptyObject(value),
    },

... 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:

ElectroDB Version
3.0.1

Expected behavior
If an attribute has the required property set to false, validation should not be called when the value is null or undefined.

@solaris007 solaris007 changed the title validation should not be called when attribute is not required and null/undefined validate should not be called when attribute is not required and null/undefined Nov 26, 2024
@tywalch
Copy link
Owner

tywalch commented Nov 27, 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 optional
  required: false,
  
  // default: () => ({ someInitialKey: "someInitialValue" }) <-- can also be a function
  default: { someInitialKey: "someInitialValue" },
  
  // validate will receive the default, and only be invoked with user-applied values for the rest of the item's life cycle
  validate: (value) => isNonEmptyObject(value),
}

@dls314
Copy link

dls314 commented Jan 3, 2025

@solaris007,

I think this is what I was seeing here: #432 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants