Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cfworker/json-schema

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cfworker/json-schema

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

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@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
    });
    

Keywords

FAQs

Package last updated on 24 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc