- a manipulate JSON Schema ES module for JavaScript
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"
- validate instance data by JSON Schema
- 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
- extract a example instance from JSON Schema
- 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 }
]
deno test -A