Skip to content

code4fukui/JSONSchema

Repository files navigation

JSONSchema.js

import { JSONSchema } from "https://code4fukui.github.io/JSONSchema/JSONSchema.js";

const schema = {
  "$schema": "http://json-schema.org/draft-07/hyper-schema#",
  "title": "Person",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "altname": {
      "type": [ "string", "null" ],
    },
    "age": {
      "type": "integer",
    },
    "friends": {
      "type": "array",
      "items": {
        "type": "string",
      }
    }
  },
  "required": [ "name", "altname" ]
};
const res = await JSONSchema.validate({ name: "taisukef" }, schema);
console.log(res.errors); // requires property "altname"

Functions

async JSONSchema.validate(instance, schema)

  • validate instance data by JSON Schema

JSONSchema.toDTS / schema2dts

  • A converter function JSON Schema to TypeScript type definition file (d.ts)
const dts = JSONSchema.toDTS(schema);
console.log(dts);
/*
export interface Person {
  name: string;
  altname: string | null;
  age?: number;
  friends?: string[];
};
*/
cd demo
deno run -A example.js > example.d.ts

JSONSchema.getExample / schema2json

  • extract a example instance from JSON Schema

JSONSchema.fromJSON(schemajson)

  • create JSON Schema by SchemaJSON json data
  • see also SchemaCSV
[
  { "name": "did", "type": "string", "example": "AA383838", "description": "publickey" },
  { "name": "name", "type": "string", "example": "taisukef" },
  { "name": "age", "type": "integer", "example": 43 }
]

Test

deno test -A

Dependencies

About

a manipulate JSON Schema ES module for JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages