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

ajv-simple-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv-simple-wrapper

Improved class interface for ajv

  • 0.1.5
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

ajv-simple-wrapper

Provides a simple class wrapper for ajv to allow use of ajv in a way that I prefer for simple validation.

Usage

Install ajv and ajv-simple-wrapper.

npm install ajv ajv-simple-wrapper

Import ajv-simple-wrapper, initialize, and validate.

import { AjvSimpleWrapper } from 'ajv-simple-wrapper';

const schema = {
  type: 'object'
  properties: {
    foo: {
      type: 'string'
    },
    bar: {
      type: 'number'
    }
  }
};
const validator = new AjvSimpleWrapper(schema);

try {
  const result = validator.validate({
    foo: 'a string',
    bar: 'not a number!',
  });
} catch (err) {
  console.error('Validation failed!', err);
}

Error Handling

Alternatively, you can initialize your ajv wrapper with a configuration of throwOnValidationError: false to not throw when an error occurs.

import { AjvSimpleWrapper } from 'ajv-simple-wrapper';

const schema = {
  type: 'object'
  properties: {
    foo: {
      type: 'string'
    },
    bar: {
      type: 'number'
    }
  }
};
const validator = new AjvSimpleWrapper(schema, { throwOnValidationError: false });

const result = validator.validate({
  foo: 'a string',
  bar: 'not a number!',
});

if (!result) {
  console.error('Validation failed!', validator.errors);
}

Keywords

FAQs

Package last updated on 06 Feb 2021

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