New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-schema-library

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-library

Javascript tools to work with json-schema, data generation and validation

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

json-schema-library

This package offers tools and utilities to work with json-schema, create and validate data. Unfortunately, most packages, editors or validators do not care to expose basic json-schema functionality. This repository lessens the pain building tools around json-schema. The tools currently support only a basic json-schema, but will hopefully expand over time.

Instead of small memory footprint or high performance, this package focuses on exposing utilities for browser and node environments. Furthermore, current validation and retrieval functions perform a lazy check, which is required for form evaluation in i.e. json editors.

npm i json-schema-library.

This package is tested on node v6.9.1.

Overview

refer to the unit-tests for up to date examples of each function.

getSchema(schema, pointer, data)

return the json 'schema' matching 'data' at 'pointer'. Should be modified to use a step/next-function, which is already within the logic (advance VS retrieve from root -> support both)

const targetSchema = getSchema(rootSchema, '#/path/to/target', rootData);

Currently may also return an error:

if (targetSchema instanceOf Error) {
    throw targetSchema;
}

SchemaService(schema)

binds the schema to getSchema.

const schemaService = new SchemaService(rootSchema);
const targetSchema = schemaService.get('#/path/to/target', rootData);

getTemplate(schema, data, rootSchema = schema)

return data which is valid to the given json-schema. Additionally, a data object may be given, which will be extended by any missing items or properties.

const baseData = getTemplate(
    { type: "object", properties: { target: { type: "string", default: "v" } } },
    { other: true }
); // returns { other: true, target: "v" }

createSchemaOf(data)

returns a json schema which is valid against data.

const baseSchema = getTemplate({ target: "" });
// returns {type: "object", properties: { target: "string"}},

isValid(data, schema)

returns the schema if it validates the data, else returns false if the data is invalid

const baseSchema = isValid("", { type: "number" });
// returns false

step(key, schema, data, rootSchema = schema)

returns the child schema found at the given key

const baseSchema = step(
    "target", 
    { type: "object", properties: { target: {type: "string"}} },
    { target: "value" }
); // returns {type: "string"}

each(data, schema, callback)

calls the callback on each item (object, array and value), passing the current schema and its data

const schema = {
    type: "array",
    items: [
        { type: "number" },
        { type: "string" }
    ]
};

each([5, "nine"], schema, (schema, value, pointer) => {
    // 1. schema = { type: "array", items: [...] }, data = [5, "nine"], pointer = #
    // 2. schema = { type: "number" }, data = 5, pointer = #/0
    // 3. schema = { type: "string" }, data = "nine", pointer = #/1
});

FAQs

Package last updated on 10 Nov 2016

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