Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
RunKit + npm: ajv
node v18.11.0
endpoint
share
tweet
const Ajv = require("ajv") const ajv = new Ajv({allErrors: true}) const schema = { type: "object", properties: { foo: {type: ["array","null"], minItems: 1}, }, required: ["foo"] } const validate = ajv.compile(schema) test({foo: null}) // valid test({foo: [1]}) // valid test({foo: []}) // invalid test({bar: 123}) // invalid function test(data) { const valid = validate(data) if (valid) console.log("Valid!") else console.log("Invalid: " + ajv.errorsText(validate.errors)) }
Created from: https://npm.runkit.com/ajv
Loading…
Clone and edit this document