-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
optimize enums with a single value #663
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Good spot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will create a difference between the logic of: enum with one value and enum with multiple values. You need to check that data value is equal to schema one before inserting it.
You can read this thread for more details. |
@ivan-tymoshenko this seems a bit odd since you don't do similar checks for things like |
The problem is how the serialization function would behave in case when it receives a value that is not in the enum. const schema = { type: 'string', enum: ['foo', 'bar'] }
const serialize = fjs(schema)
serialize('baz') // should return baz IIRC const schema = { type: 'string', enum: ['foo'] }
const serialize = fjs(schema)
serialize('baz') // will return 'foo' IMO this is confusing. I agree that situation with You still will have some performance boost if you add a sanity check. |
My few recomendations on adding enum support:
This will give you the performance boost, because you will avoid the string sanitizing in a hot path. |
@ivan-tymoshenko I did read that thread but it didn't enlighten me; per https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.3
|
Can you tell me what this library should do if it receives a value that is not in the enum? |
@ivan-tymoshenko Per the specification, having a value outside of the single value of the enum means that the value passed in isn't valid and is seemingly subject to the README:
Additionally from that thread linked:
So sending values outside of those explicit values (including
Would actually produce an invalid value for the schema and should not be done. Since these are values are outside of valid values for the schema it seems likely to be up to implementer desires according to the README and thread linked; I don't think checking the value is needed given these points existing. I'm not a maintainer so it is hard to say what is desired by maintainers, but that the spec call out of equivalence isn't valid here is surprising. My only comment is that likely Calling out and validating enums is certainly possible to hot path outside of single values with some steps like you suggest above but seems out of my general cause of making this PR which was one of surprise. |
Checklist
npm run test
andnpm run benchmark
documentation is changed or addedand the Code of conduct
Before and after log: