Skip to main content
Module

x/fun/mod.ts>json_schema.lazy

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function json_schema.lazy
import { json_schema } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { lazy } = json_schema;

Creates a lazy JsonBuilder, which is useful for creating recursive JSON Schemas, mutual or otherwise. A limitation of typescript means that the type must be defined outside of the JsonBuilder and manually annotated

Examples

Example 1

import * as J from "./json_schema.ts";
import { pipe } from "./fn.ts";

// {
//   "$ref": "#/definitions/Person",
//   "definitions": {
//     "Person": {
//       "type": "object",
//       "properties": {
//         "age": {
//           "type": "number"
//         },
//         "children": {
//           "type": "array",
//           "items": {
//             "$ref": "#/definitions/Person"
//           }
//         },
//         "name": {
//           "type": "string"
//         }
//       },
//       "required": [
//         "age",
//         "children",
//         "name"
//       ]
//     }
//   }
// }

type Person = {
  readonly name: string;
  readonly age: number;
  readonly children: ReadonlyArray<Person>;
};

const Person: J.JsonBuilder<Person> = J.lazy("Person", () =>
  J.struct({
    name: J.string(),
    age: J.number(),
    children: J.array(Person),
  }));

const schema = J.print(Person);

Parameters

id: string
fn: () => JsonBuilder<A>