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

awesome-ajv-errors

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awesome-ajv-errors

Prettified AJV errors

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37K
increased by22.63%
Maintainers
1
Weekly downloads
 
Created
Source

npm version downloads build status coverage status Language grade: JavaScript Node.JS version

awesome-ajv-errors pretty-prints ajv errors

It has a gorgeous human-understandable output, predicts human errors and suggests fixes.

Contents

Examples

Similar property name

Suggest similar properties

JSON Schema and data

schema.json

{
  "title": "Second-level two similar properties",
  "type": "object",
  "properties": {
    "foo": {
      "type": "object",
      "properties": {
        "bar": {},
        "bak": {}
      },
      "additionalProperties": false
    }
  }
}

data.json

{
  "foo": {
    "bar": "42",
    "baz": "33"
  }
}

Multiple similar property names

Suggests multiple valid property names

JSON Schema and data

schema.json

{
  "title": "Second-level three similar properties",
  "type": "object",
  "properties": {
    "foo": {
      "type": "object",
      "properties": {
        "bar": {},
        "bak": {},
        "bam": {}
      },
      "additionalProperties": false
    }
  }
}

data.json

{
  "foo": {
    "bar": "42",
    "baz": "33"
  }
}

Type typo

Suggests the valid value type when mistaken

JSON Schema and data

schema.json

{
  "title": "One option (number to string)",
  "type": "object",
  "properties": {
    "foo": {
      "anyOf": [
        {
          "type": "string"
        }
      ]
    }
  }
}

data.json

{
  "foo": 42
}

Type typo (reverse)

Suggests the valid value type when mistaken

JSON Schema and data

schema.json

{
  "title": "One option (string to number)",
  "type": "object",
  "properties": {
    "foo": {
      "anyOf": [
        {
          "type": "number"
        }
      ]
    }
  }
}

data.json

{
  "foo": "42"
}

Type typo (one much better option)

When the type mismatch, and one type is much "better" than the rest (as in probably the right solution), it will be suggested for conversion

JSON Schema and data

schema.json

{
  "title": "Two options",
  "type": "object",
  "properties": {
    "foo": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "boolean"
        }
      ]
    }
  }
}

data.json

{
  "foo": 42
}

Type typo (one much better option out of multiple)

JSON Schema and data

schema.json

{
  "title": "Three options",
  "type": "object",
  "properties": {
    "foo": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        }
      ]
    }
  }
}

data.json

{
  "foo": 42
}

Array too small

JSON Schema and data

schema.json

{
  "title": "2 too few",
  "type": "object",
  "properties": {
    "foo": {
      "type": "array",
      "minItems": 3
    }
  }
}

data.json

{
  "foo": [
    1
  ]
}

Number too big

JSON Schema and data

schema.json

{
  "title": "Less than or equal to",
  "type": "object",
  "properties": {
    "foo": {
      "type": "number",
      "maximum": 17
    }
  }
}

data.json

{
  "foo": 42
}

Not in enum set

JSON Schema and data

schema.json

{
  "title": "One value of same type",
  "type": "object",
  "properties": {
    "foo": {
      "enum": [
        41
      ]
    }
  }
}

data.json

{
  "foo": 42
}

Almost in enum set (wrong convertible type)

JSON Schema and data

schema.json

{
  "title": "Two options (one of different type)",
  "type": "object",
  "properties": {
    "foo": {
      "enum": [
        41,
        "42"
      ]
    }
  }
}

data.json

{
  "foo": 42
}

Almost in enum set (wrong convertible type), multiple options

JSON Schema and data

schema.json

{
  "title": "Four options (one of different type)",
  "type": "object",
  "properties": {
    "foo": {
      "enum": [
        "falso",
        "other",
        "False",
        false
      ]
    }
  }
}

data.json

{
  "foo": "false"
}

Invalid format (time)

JSON Schema and data

schema.json

{
  "title": "time invalid",
  "type": "object",
  "properties": {
    "foo": {
      "type": "string",
      "format": "time"
    }
  }
}

data.json

{
  "foo": "11:22:334"
}

Invalid format (e-mail)

JSON Schema and data

schema.json

{
  "title": "email invalid",
  "type": "object",
  "properties": {
    "foo": {
      "type": "string",
      "format": "email"
    }
  }
}

data.json

{
  "foo": "quite@invalid@email.com"
}

if-then not satisfied

JSON Schema and data

schema.json

{
  "title": "if-then on first-level object",
  "properties": {
    "foo": {
      "if": {
        "properties": {
          "firstName": {
            "const": true
          }
        }
      },
      "then": {
        "required": [
          "lastName"
        ]
      }
    }
  }
}

data.json

{
  "foo": {
    "firstName": true
  }
}

Multiple of

JSON Schema and data

schema.json

{
  "title": "Multiple of",
  "type": "object",
  "properties": {
    "foo": {
      "type": "number",
      "multipleOf": 4
    }
  }
}

data.json

{
  "foo": 17
}

Required property

JSON Schema and data

schema.json

{
  "title": "Root-level required",
  "type": "object",
  "properties": {
    "foo": {}
  },
  "required": [
    "foo"
  ]
}

data.json

{
  "bar": 42
}

Usage

Import the ajv package, and prettify from awesome-ajv-errors:

import * as Ajv from 'ajv'
import { prettify } from '../'

Create an ajv instance and validate objects:

const ajv = new Ajv( { allErrors: true } ); // allErrors is optional

let data, schema; // Get the JSON schema and the JSON data from somewhere

const validate = ajv.compile( schema );
validate( data );

Now, the validation error is stored on the validate function. Use prettify to pretty-print the errors, and provide the data so that awesome-ajv-errors can suggest fixes:

console.log( prettify( validate, { data } ) );

Keywords

FAQs

Package last updated on 22 Mar 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