New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-sv

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-sv

JSON Schema Validator

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

JSON Schema Validator

Validate you JSON now!

How to?

import { group, object, required, string } from "json-sv";

const test = object({
    firstName: string(),
    lastName: group([
        required(),
        string(),
    ]).when(required().ref("firstName")),
});

const errors = test.errors({
    firstName: "Ivan",
});

console.log("Errors:", errors);

Format

Uses JSON format for a Schema declaration. For example:

{
    "$type": "object",
    "keys": {
        "a": {
            "$type": "required"
        },
        "b": {
            "$type": "required",
            "$when": {
                "$type": "required",
                "$context": ["a"]
            }
        }
    }
}

Base Validator

A base Validators declaration should contains follow propertie: $type - string, one of available validators name. If you want to declare conditionaly validator (should be current validator execut, or not), use $when - schema. When you uses $when, internal conditional validator checks same data (from parent validator), for changing validation context, use $context - array of string, a route in root data.

type ISchemaContext = string[];

interface ISchema {
    $type: string;
    $when?: ISchema;
    $context?: ISchemaContext;
}

Parse/Stringify/Objectify

You can parse a object declaration or a JSON string to the Validator. Also actually you can create the object declaration or the JSON string from a Validator.

import { Schema, group, object, required, string } from "schema-validator";

const subSchema = Schema.parse(mySchemaJsonStringOrObject);
const schema = object({
    name: group([
        required(),
        string(),
    ]),
    data: subSchema,
});

const finalSchemaObject = Schema.objectify(schema);
const finalSchemaObject_1 = schema.schema;

const finalSchemaJson = Schema.stringify(schema);
const finalSchemaJson_1 = JSON.stringify(schema.schema);

Keywords

schema

FAQs

Package last updated on 19 Nov 2018

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