Socket
Book a DemoInstallSign in
Socket

@geoblink/ajv-extra

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@geoblink/ajv-extra

Extra goods for ajv

1.0.22
latest
npmnpm
Version published
Maintainers
1
Created
Source

Ajv extra is a drop in replacement for ajv with extra goods.

Installation

const Ajv = require('@geoblink/ajv-extra')
const ajv = new Ajv({ allErrors: true })

const schema = { /*** a valid JSON schema ***/ }
validator = ajv.compile(schema)

const isValid = validator({ /*** JSON object to be validated ***/})

OneOfByKey

Sometimes the schema is parametrized by some information. For example:

const schema = {
  type: 'object',
  required: ['data'],
  properties: {
    data: {
      oneOf: [
        {
          type: 'object',
          properties: {
            origin: {
              enum: ['external']
            },
            /** some external properties **/
          }
        },
        {
          type: 'object',
          properties: {
            origin: {
              enum: ['internal']
            },
            /** some external properties **/
          }
        },
      ]
    }
  }
}

In the previous example, we receive data that can be either internal or external with a schema that depends on origin. This schema is nice to work with because data is always present. Depending on the origin we'll have one or the other.

However, there is a big hindrance. Logs are really ugly. If we are given:

const obj = {
  data: {
    origin: 'internal',
    /** wrong properties for internal **/
  }
}

ajv has will show the logs for internal and external, although we are only interested in the internal properties. To solve this problem, we can use oneOfByKey which has a very similar syntax

const schema = {
  type: 'object',
  required: ['data'],
  properties: {
    data: {
      oneOfByKey: {
        key: 'origin',
        oneOf: [
          {
            type: 'object',
            properties: {
              origin: {
                enum: ['external']
              },
              /** some external properties **/
            }
          },
          {
            type: 'object',
            properties: {
              origin: {
                enum: ['internal']
              },
              /** some external properties **/
            }
          },
        ]
      }
    }
  }
}

Now the validator knows that we are only interested in errors for internal properties

FAQs

Package last updated on 08 Nov 2019

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.