Last active
May 27, 2024 13:50
-
-
Save nflaig/cd1cf5a03f9da6bcb055d639ddc8a6dd to your computer and use it in GitHub Desktop.
openapi oneOf array example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ajv from "ajv"; | |
const ajv = new Ajv({ strict: true }); | |
const schema = { | |
type: "object", | |
properties: { | |
data: { | |
oneOf: [ | |
{ type: "array", items: { type: "string" } }, | |
{ type: "array", items: { type: "number" } }, | |
], | |
}, | |
}, | |
}; | |
const validate = ajv.compile(schema); | |
console.log(validate({ data: ["a", 1] })); // invalid | |
console.log(validate({ data: ["a", "b"] })); // valid | |
console.log(validate({ data: [1, 2] })); // valid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ajv from "ajv"; | |
const ajv = new Ajv({ strict: true }); | |
const schema = { | |
type: "object", | |
properties: { | |
data: { | |
type: "array", | |
items: { oneOf: [{ type: "string" }, { type: "number" }] }, | |
}, | |
}, | |
}; | |
const validate = ajv.compile(schema); | |
console.log(validate({ data: ["a", 1] })); // valid | |
console.log(validate({ data: ["a", "b"] })); // valid | |
console.log(validate({ data: [1, 2] })); // valid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment