Gathering detailed insights and metrics for @swenkerorg/sed-dolores
Gathering detailed insights and metrics for @swenkerorg/sed-dolores
Gathering detailed insights and metrics for @swenkerorg/sed-dolores
Gathering detailed insights and metrics for @swenkerorg/sed-dolores
npm install @swenkerorg/sed-dolores
Typescript
Module System
Node Version
NPM Version
57.9
Supply Chain
83.6
Quality
86
Maintenance
100
Vulnerability
99.3
License
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1,940 Commits
1 Watching
1 Branches
1 Contributors
Latest Version
4.7.98
Package Id
@swenkerorg/sed-dolores@4.7.98
Unpacked Size
208.86 kB
Size
108.18 kB
File Count
560
NPM Version
10.8.2
Node Version
20.17.0
Publised On
10 Sept 2024
Cumulative downloads
Total Downloads
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
42
@swenkerorg/sed-dolores
A code-generating JSON Schema validator that attempts to be reasonably secure.
Supports draft-04/06/07/2019-09/2020-12 and the
discriminator
OpenAPI keyword.
requireValidation: true
mode enforces full validation of the input object.mode: "strong"
is recommended, — it combines that option with additional schema safety checks.{type: "array", "maxLength": 2}
.discriminator
OpenAPI keyword.1npm install --save @swenkerorg/sed-dolores
Simply pass a schema to compile it:
1const { validator } = require('@swenkerorg/sed-dolores') 2 3const validate = validator({ 4 type: 'object', 5 required: ['hello'], 6 properties: { 7 hello: { 8 type: 'string' 9 } 10 } 11}) 12 13console.log('should be valid', validate({ hello: 'world' })) 14console.log('should not be valid', validate({}))
Or use the parser API (running in strong mode by default):
1const { parser } = require('@swenkerorg/sed-dolores') 2 3const parse = parser({ 4 $schema: 'https://json-schema.org/draft/2019-09/schema', 5 type: 'object', 6 required: ['hello'], 7 properties: { 8 hello: { 9 pattern: '^[a-z]+$', 10 type: 'string' 11 } 12 }, 13 additionalProperties: false 14}) 15 16console.log(parse('{"hello": "world" }')) // { valid: true, value: { hello: 'world' } } 17console.log(parse('{}')) // { valid: false }
Parser API is recommended, because this way you can avoid handling unvalidated JSON objects in non-string form at all in your code.
See options documentation for the full list of supported options.
@swenkerorg/sed-dolores
supports the formats specified in JSON schema v4 (such as date-time).
If you want to add your own custom formats pass them as the formats options to the validator:
1const validate = validator({ 2 type: 'string', 3 format: 'no-foo' 4}, { 5 formats: { 6 'no-foo': (str) => !str.includes('foo'), 7 } 8}) 9console.log(validate('test')) // true 10console.log(validate('foo')) // false 11 12const parse = parser({ 13 $schema: 'https://json-schema.org/draft/2019-09/schema', 14 type: 'string', 15 format: 'only-a' 16}, { 17 formats: { 18 'only-a': /^a+$/, 19 } 20}) 21console.log(parse('"aa"')) // { valid: true, value: 'aa' } 22console.log(parse('"ab"')) // { valid: false }
You can pass in external schemas that you reference using the $ref
attribute as the schemas
option
1const ext = { 2 type: 'string' 3} 4 5const schema = { 6 $ref: 'ext#' // references another schema called ext 7} 8 9// pass the external schemas as an option 10const validate = validator(schema, { schemas: { ext: ext }}) 11 12console.log(validate('hello')) // true 13console.log(validate(42)) // false
schemas
can be either an object as shown above, a Map
, or plain array of schemas (given that
those have corresponding $id
set at top level inside schemas themselves).
When the includeErrors
option is set to true
, @swenkerorg/sed-dolores
also outputs:
keywordLocation
: a JSON pointer string as an URI fragment indicating which sub-schema failed, e.g.
#/properties/item/type
instanceLocation
: a JSON pointer string as an URI fragment indicating which property of the object
failed validation, e.g. #/item
1const schema = { 2 type: 'object', 3 required: ['hello'], 4 properties: { 5 hello: { 6 type: 'string' 7 } 8 } 9} 10const validate = validator(schema, { includeErrors: true }) 11 12validate({ hello: 100 }); 13console.log(validate.errors) 14// [ { keywordLocation: '#/properties/hello/type', instanceLocation: '#/hello' } ]
Or, similarly, with parser API:
1const schema = { 2 $schema: 'https://json-schema.org/draft/2019-09/schema', 3 type: 'object', 4 required: ['hello'], 5 properties: { 6 hello: { 7 type: 'string', 8 pattern: '^[a-z]+$', 9 } 10 }, 11 additionalProperties: false, 12} 13const parse = parser(schema, { includeErrors: true }) 14 15console.log(parse('{ "hello": 100 }')); 16// { valid: false, 17// error: 'JSON validation failed for type at #/hello', 18// errors: [ { keywordLocation: '#/properties/hello/type', instanceLocation: '#/hello' } ] 19// }
Only the first error is reported by default unless allErrors
option is also set to true
in
addition to includeErrors
.
See Error handling for more information.
See the doc/samples directory to see how @swenkerorg/sed-dolores
compiles
supported test suites.
To compile a validator function to an IIFE, call validate.toModule()
:
1const { validator } = require('@swenkerorg/sed-dolores') 2 3const schema = { 4 type: 'string', 5 format: 'hex' 6} 7 8// This works with custom formats as well. 9const formats = { 10 hex: (value) => /^0x[0-9A-Fa-f]*$/.test(value), 11} 12 13const validate = validator(schema, { formats }) 14 15console.log(validate.toModule()) 16/** Prints: 17 * (function() { 18 * 'use strict' 19 * const format0 = (value) => /^0x[0-9A-Fa-f]*$/.test(value); 20 * return (function validate(data) { 21 * if (data === undefined) data = null 22 * if (!(typeof data === "string")) return false 23 * if (!format0(data)) return false 24 * return true 25 * })})(); 26 */
@swenkerorg/sed-dolores
uses code generation to turn a JSON schema into javascript code that is easily
optimizeable by v8 and extremely fast.
See Performance for information on options that might affect performance both ways.
Get a fully set up development environment with:
1git clone https://github.com/swenkerorg/sed-dolores 2cd schemasafe 3 4git submodule update --init --recursive 5yarn 6yarn lint 7yarn test
This is based on a heavily rewritten version of the amazing (but outdated) is-my-json-valid by @mafintosh.
Compared to is-my-json-valid
, @swenkerorg/sed-dolores
adds security-first design, many new features,
newer spec versions support, slimmer and more maintainable code, 0 dependencies, self-contained JS
module generation, fixes bugs and adds better test coverage, and drops support for outdated Node.js
versions.
No vulnerabilities found.
No security vulnerabilities found.