JSPM

  • Created
  • Published
  • Downloads 1533276
  • Score
    100M100P100Q208464F
  • License MIT

A JSON schema validator that will run on Cloudflare workers. Supports drafts 4, 7, and 2019-09.

Package Exports

  • @cfworker/json-schema

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@cfworker/json-schema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@cfworker/json-schema

A JSON schema validator that will run on Cloudflare workers. Supports drafts 4, 7, and 2019-09.

background

Why another JSON schema validator?

Cloudflare workers do not have APIs required by Ajv schema compilation (eval or new Function(code)). If possible use Ajv in a build step to precompile your schema. Otherwise use this library.

usage

  1. Basic

    import { Validator } from '@cfworker/json-schema';
    
    const validator = new Validator({ type: 'number' });
    
    const result = validator.validate(7);
  2. Specify meta schema draft

    const validator = new Validator({ type: 'number' }, '4'); // draft-4
  3. Add schemas

    const validator = new Validator({
      $id: 'https://foo.bar/baz',
      $ref: '/beep'
    });
    
    validator.addSchema({ $id: 'https://foo.bar/beep', type: 'boolean' });
  4. Include all errors By default the validator will stop processing an object or array after the first error. Specifying shortCircuit to false will produce all errors.

    const validator = new Validator(
      {
        type: 'object',
        properties: {
          name: { type: 'string' },
          email: { type: 'string' },
          number: { type: 'number' },
          required: ['name', 'email', 'number']
        }
      },
      '2019-09',
      false
    );
    
    const result = validator.validate({
      name: 'hello',
      email: 5, //invalid type
      number: 'Hello' //invalid type
    });