JSON Schema

1 min read

A global schema.validate method is provided to validate JSON data with a JSON Schema.

validate: (
  input: any,
  schema: Schema
) => Promise<void>;

This method throws an error when invalid.

Usage example:

const input = 'Invalid because not wrapped in an object';

const schema = {
  type: 'object',
  properties: {
    title: {type: 'string', maxLength: 64}
  },
  required: ['string']
};

let valid;
try {
  await schema.validate(input, schema);
  valid = true;
} catch (err) {
  console.error(err.message);
  valid = false;
}