🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

json-validator-util

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-validator-util

This is utility package that helps in validating the json schema

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
46
-36.11%
Maintainers
1
Weekly downloads
 
Created
Source

JSON Validator

This is utility package that helps in validating the json schema. It uses ajv package internally to validate

Features

  • Validate through json schema
  • Validate through json filename

Installation

  npm i json-validator-util
  OR
  yarn json-validator-util

Usage/Examples

Usage

// validating through schema directly
import { validator } from 'json-validator-util';

const response = validator.validate(payload, jsonSchema);
if(!response.isValid) {
    console.log(response.errors);
}
// validating through schema filename
import { validator } from 'json-validator-util';

validator.initialize({
    schemaPath: 'relative/path/to/folder'
})

const response = validator.validate(payload, 'remaining_path/filename.json');
if(!response.isValid) {
    console.log(response.errors);
}
// if need to get ajv original error then pass third parameter value as true

const response = validator.validate(first_param, second_param, true);
if(!response.isValid) {
    console.log(response.errors); // will print original ajv style error if validation fails
}

Example

import { validator } from 'json-validator-util';

const payload = {
    foo: 123,
    bar: "value"
};
const jsonSchema = {
  "type": "object",
  "properties": {
    "foo": {
      "type": "integer"
    },
    "bar": {
      "type": "string"
    }
  },
  "required": [
    "foo"
  ],
  "additionalProperties": false
}

const response = validator.validate(payload, jsonSchema);
if(!response.isValid) {
    console.log(response.errors);
}

FAQs

Package last updated on 18 Aug 2023

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