$ npm install @hoseung.json/schema
import { S } from "@hoseung.json/schema";
// {
// type: "object",
// properties: {
// id: {
// type: "number"
// },
// name: {
// type: "string"
// }
// }
// required: [
// "id",
// "name"
// ]
// }
const User = S.Object({
id: S.Number(),
name: S.String(),
});
// {
// id: number;
// name: string;
// }
type User = S.Resolve<typeof User>;
if (User.match(unknownValue)) {
// type of unknownValue is:
// {
// id: number;
// name: string;
// }
}
You can create a JSON schema using functions below.
A function which creates a number JSON schema
// {
// type: "number"
// }
const Num = S.Number();
// number
type Num = S.Resolve<typeof Num>;
A function which creates a string JSON schema
// {
// type: "string"
// }
const Str = S.String();
// string
type Str = S.Resolve<typeof Str>;
A function which creates a boolean JSON schema
// {
// type: "boolean"
// }
const Bool = S.Boolean();
// boolean
type Bool = S.Resolve<typeof Bool>;
A function which creates a null JSON schema
// {
// type: "null"
// }
const Null = S.Null();
// null
type Null = S.Resolve<typeof Null>;
A function which creates an array JSON schema
// {
// type: "array",
// items: {
// type: "number"
// }
// }
const Array = S.Array(S.Number());
// number[]
type Array = S.Resolve<typeof Array>;
A function which creates an object JSON schema
// {
// type: "object",
// properties: {
// a: {
// type: "number"
// },
// b: {
// type: "string"
// }
// },
// required: [
// "a",
// "b"
// ]
// }
const Obj = S.Object({
a: S.Number(),
b: S.String(),
});
// {
// a: number;
// b: string;
// }
type Obj = S.Resolve<typeof Obj>;
A function which creates a number/string/boolean JSON schema with constant value
// {
// type: "number",
// const: "foo"
// }
const Literal = S.Literal(123);
// 123
type Literal = S.Resolve<typeof Literal>;
// {
// type: "string",
// const: "foo"
// }
const Literal = S.Literal("foo");
// "foo"
type Literal = S.Resolve<typeof Literal>;
// {
// type: "boolean",
// const: true
// }
const Literal = S.Literal(true);
// true
type Literal = S.Resolve<typeof Literal>;
A function which marks a property in an object JSON schema as optional
// {
// type: "object",
// properties: {
// a: {
// type: "number"
// },
// b: {
// type: "string"
// }
// },
// required: [
// "a"
// ]
// }
const Optional = S.Object({
a: S.Number(),
b: S.Optional(S.String()),
});
// {
// a: number;
// b?: string;
// }
type Optional = S.Resolve<typeof Optional>;
A function which creates an anyOf JSON schema
// {
// anyOf: [
// {
// type: "number"
// },
// {
// type: "string"
// }
// ]
// }
const Union = S.Union([S.Number(), S.String()]);
// number | string
type Union = S.Resolve<typeof Union>;
A function which creates an object JSON schema only with additionalProps
// {
// type: "object",
// properties: {},
// additionalProperties: {
// type: "number"
// }
// }
const Dict = S.Dict(S.Number());
// {
// [key: string]: number;
// }
type Dict = S.Resolve<typeof Dict>;
You may need to validate what kind of a Schema is. So there are validation function which named is{Schema Name}
for all kinds of Schema.
S.isNumber(S.Number()); // true
S.isNumber(S.String()); // false
if (S.isNumber(unknownSchema)) {
// type of unknownSchema is NumberSchema
}
You may need to validate if a value matches a Schema. So all kinds of Schema have match
function.
const Locale = S.Union([S.Literal("ko"), S.Literal("en")]);
if (Locale.match(unknownValue)) {
// type of unknownValue is "ko" | "en"
}