Gathering detailed insights and metrics for @exodus/schemasafe
Gathering detailed insights and metrics for @exodus/schemasafe
Gathering detailed insights and metrics for @exodus/schemasafe
Gathering detailed insights and metrics for @exodus/schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
npm install @exodus/schemasafe
99.3
Supply Chain
100
Quality
78.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
162 Stars
988 Commits
12 Forks
27 Watching
7 Branches
96 Contributors
Updated on 18 Nov 2024
JavaScript (99.75%)
TypeScript (0.25%)
Cumulative downloads
Total Downloads
Last day
-6.8%
313,775
Compared to previous day
Last week
2.8%
1,762,522
Compared to previous week
Last month
7%
7,423,053
Compared to previous month
Last year
69.4%
71,178,562
Compared to previous year
@exodus/schemasafe
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 @exodus/schemasafe
Simply pass a schema to compile it:
1const { validator } = require('@exodus/schemasafe') 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('@exodus/schemasafe') 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.
@exodus/schemasafe
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
, @exodus/schemasafe
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 @exodus/schemasafe
compiles
supported test suites.
To compile a validator function to an IIFE, call validate.toModule()
:
1const { validator } = require('@exodus/schemasafe') 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 */
@exodus/schemasafe
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/ExodusMovement/schemasafe 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
, @exodus/schemasafe
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.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 8/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More