RunKit + npm: ajv

node v18.11.0
endpointsharetweet
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…