validator: introduce experimental node:validator module#62927
validator: introduce experimental node:validator module#62927sheplu wants to merge 4 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
b7d3be2 to
6fdf84e
Compare
6fdf84e to
3f99703
Compare
3f99703 to
9454350
Compare
|
Unfortunately JSON Schema doesn't support many of the things that JS apps need to validate - functions, regexes, for some examples, so the generic name "validator" doesn't seem like a good fit if it'd only cover JSON-serializable data. |
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62927 +/- ##
==========================================
+ Coverage 89.63% 89.67% +0.03%
==========================================
Files 706 712 +6
Lines 219219 220181 +962
Branches 42004 42259 +255
==========================================
+ Hits 196499 197438 +939
- Misses 14622 14637 +15
- Partials 8098 8106 +8
π New features to boost your workflow:
|
|
A bit of overlap with the ata effort in #62603. Might want to just expose that instead given the measured perf. |
|
Hey @sheplu, good to see this come up. We've been on the same track. Context: #62603 is vendoring ata into core for On @ljharb's naming point, agreed, Going to open a tracking issue shortly and drop the link here. Syncing with @H4ad later today, happy to pull you in if you want to compare notes. Appreciate you kicking this off. Rather keep these in sync than run two efforts in parallel. |
|
Seems like I was bad at searching for something similar! Thanks @Qard @mertcanaltin definitely, feel free to include me! More that happy to help work on that. And yes the naming was broad :D |
|
:D The issue being followed is currently open: #62598, I'm awaiting your comments. |
|
@sheplu No worries. I've just been reviewing it so had it already in my mind when I saw this. I actually assumed at first that this was just going to be the work to expose that, since I knew it had not been done yet. π |
|
I feel like asking input from people who explored this topic along the years would be valuable? Friendly ping to @colinhacks from zod and @ssalbdivad from ArkType who may have a few ideas to share on design of such validation API in Node. |
mcollina
left a comment
There was a problem hiding this comment.
I'm not convinced this belongs to core. The ecosystem is having constant innovation on this area in the last few years, and solidifying something in core like this would hamper it.
Introduces a new experimental built-in module
node:validator- a synchronous, JSON-Schema-inspired input validator for simple REST-style payload checks.Gated behind
--no-experimental-validator(on by default, opt-out) and emits anExperimentalWarningon first require.The purpose of this PR is not reflecting what a fully implemented validator would be able to handle, but to have a discussion to know if this make sense to continue in this line.
Key Features
Schemaclass: validate and normalize a schema once, reuse for many validations.string,number,integer,boolean,object,array,null.minLength/maxLength/pattern/enumfor strings,minimum/maximum/exclusiveMinimum/exclusiveMaximum/multipleOffor numbers,minItems/maxItems/itemsfor arrays,properties/required/additionalPropertiesfor objects.schema.validate(data)returns a frozen{ valid, errors }result; never throws.codesexport (e.g.INVALID_TYPE,STRING_TOO_SHORT,PATTERN_MISMATCH).schema.applyDefaults(data)returns a new object with declared defaults applied (input is not mutated).schema.toJSON()returns a frozen copy of the definition so schemas can be embedded in other schemas.ERR_VALIDATOR_INVALID_SCHEMAatnew Schema(...), not at validate time.Example
Example β validate a user payload
Example β apply defaults
A bit of the chicken/egg with the linter, so I also opened nodejs/core-validate-commit#147 and for now using the
node(validator)commit message