validator: introduce experimental node:validator module#62927
validator: introduce experimental node:validator module#62927sheplu wants to merge 3 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.04%
==========================================
Files 706 712 +6
Lines 219219 219999 +780
Branches 42004 42199 +195
==========================================
+ Hits 196499 197295 +796
+ Misses 14622 14612 -10
+ Partials 8098 8092 -6
🚀 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. |
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