import { json_schema } from "https://deno.land/x/fun@v2.0.0-alpha.6/mod.ts";
const { SchemableJsonBuilder } = json_schema;
An instance of Schemable for use with a Schema.
Examples
Example 1
Example 1
import * as J from "./json_schema.ts";
import * as S from "./schemable.ts";
// Let's start with a recursive type structure
type Tree<A> = {
readonly tag: 'Tree';
readonly value: A;
readonly forest: ReadonlyArray<Tree<A>>;
}
// Next we can create a generic Schema (non-JSON) for Tree<string>
const TreeSchema: S.Schema<Tree<string>> = S.schema(s =>
s.lazy("Tree<string>", () => s.struct({
tag: s.literal("Tree"),
value: s.string(),
forest: s.array(TreeSchema(s)),
}))
);
// Then we can derive a JsonBuilder from the generic Schema
const TreeJsonBuilder = TreeSchema(J.SchemableJsonBuilder);
// Lastly, we can pull an actual JSON Schema from the JsonBuilder
const TreeJsonSchema = J.print(TreeJsonBuilder);
// {
// "$ref": "#/definitions/Tree",
// "definitions": {
// "Tree": {
// "type": "object",
// "properties": {
// "forest": {
// "type": "array",
// "items": {
// "$ref": "#/definitions/Tree"
// }
// },
// "tag": {
// "enum": [
// "Tree"
// ]
// },
// "value": {
// "type": "string"
// }
// },
// "required": [
// "forest",
// "tag",
// "value"
// ]
// }
// }
// }