Skip to content

Instantly share code, notes, and snippets.

@nflaig
Last active May 27, 2024 13:50
Show Gist options
  • Save nflaig/cd1cf5a03f9da6bcb055d639ddc8a6dd to your computer and use it in GitHub Desktop.
Save nflaig/cd1cf5a03f9da6bcb055d639ddc8a6dd to your computer and use it in GitHub Desktop.
openapi oneOf array example
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
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