Published
Edited
Jul 30, 2020
1 star
Insert cell
Insert cell
validator = ajv.compile({
oneOf: [
{type: "string", enum: ["foo", "bar"]},
{type: "null"}
]
})
Insert cell
validate("foo")
Insert cell
validate("bar")
Insert cell
validate("baz") // invalid input
Insert cell
validate(null)
Insert cell
function validate(input) {
if (!validator(input)) {
throw new ParseError(validator.errors);
}
return null;
}
Insert cell
class ParseError extends Error {
constructor(errors) {
super(`invalid input\n${errors.map(e => e.message).join("\n")}`);
this.name = ParseError.name;
this.errors = errors;
}
}
Insert cell
ajv = (await require("ajv@6/dist/ajv.min.js"))()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more