Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SchemaFactory<T, R, S>

Type parameters

Hierarchy

Index

Constructors

constructor

Properties

$schema

$schema: string = "http://json-schema.org/draft-07/schema#"

definitions

definitions: Record<string, Schema["plain"]>

isFluentSchema

isFluentSchema: true = true

isRequired

isRequired: boolean = true

otype

otype: R extends true ? T : T | undefined

plain

plain: S = {} as S

type

type: T

Static ajv

ajv: Ajv = new Ajv().addKeyword('custom', {validate: (value: string[], data: any, schema: Schema['plain'], path: string, full: any) =>value.every(key => BaseSchema.validators[key](data, full, schema, path))})

Static validators

validators: Record<string, Validator>

Methods

allOf

  • allOf<P>(...schemas: P): BaseSchema<U.IntersectOf<P[number]["type"]>, R>

any

  • any(): BaseSchema<undefined | null | string | number | false | true | {} | any[], true, {}>
  • Create an empty schema

    exmaple

    { }

    Returns BaseSchema<undefined | null | string | number | false | true | {} | any[], true, {}>

anyOf

  • anyOf<P>(...schemas: P): BaseSchema<P[number]["type"], R>

array

boolean

  • Create BooleanSchema

    example

    { "type": "boolean" }

    Returns BaseSchema<boolean, true, {}>

const

copyWith

  • copyWith(modifyObject: Partial<BaseSchema & { plain: Partial<Schema["plain"]> }>): this
  • Make copy of current schema with modified values

    Parameters

    • modifyObject: Partial<BaseSchema & { plain: Partial<Schema["plain"]> }>

    Returns this

custom

  • Add custom validation functions. Since custom validators didn't supported by JSON Schema, I used AJV custom keywords to add such functionality

    Parameters

    Returns this

default

  • default(def: T): this

definition

  • definition(name: string, definition: BaseSchema): this

description

  • description(description: string): this

ensure

  • ensure(data: T): T
  • Validate provided data with current schema using ajv, if validation failed function will throw error

    Parameters

    • data: T

    Returns T

enum

examples

  • examples(...examples: T[]): this

id

  • id($id: string): this

ifThen

ifThenElse

instanceOf

  • Check the type of the provided value. Used custom ajv keyword.

    Type parameters

    Parameters

    • Type: P

    Returns BaseSchema<true, {}>

integer

  • Create NumericSchema(integer)

    example

    { "type": "integer" }

    Returns NumericSchema<number, true>

list

  • Create ArraySchema

    example

    { "type": "array", "items": { ... } }

    Type parameters

    Parameters

    • items: T

    Returns ArraySchema<T["type"], true>

not

  • not<P>(not: P): this

null

number

object

oneOf

  • oneOf<P>(...schemas: P): BaseSchema<P[number]["type"], R>

optional

  • Should be used only with ObjectSchema. By default all properties in Object Schema are required. This will make them optional

    Returns BaseSchema<T, false>

raw

  • raw(fragment: Record<string, any>): this
  • Because the differences between JSON Schemas and Open API (Swagger) it can be handy to arbitrary modify the schema injecting a fragment

    • Examples:
    • S.number().raw({ nullable:true })
    • S.string().format('date').raw({ formatMaximum: '2020-01-01' })
    example

    { somethingCustom: 'value' }

    reference

    https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.3.3

    Parameters

    • fragment: Record<string, any>

      an arbitrary JSON Schema to inject

    Returns this

ref

  • ref($ref: string): this
  • The value must be a valid id e.g. #properties/foo

    example

    { $ref: "string" }

    Parameters

    • $ref: string

    Returns this

schema

  • schema($schema: string): this
  • Set $schema property

    example

    { $schema: "string" }

    Parameters

    • $schema: string

    Returns this

shape

  • shape<T>(props: T, additional?: boolean): ObjectSchema<{ 1: { [K in keyof { [K in keyof T]: T[K]["otype"]; }]?: { [K in keyof T]: T[K]["otype"]; }[K] | undefined; }; 0: Compute<{ [K in keyof Pick<{ [K in keyof T]: T[K]["otype"]; }, SelectKeys<{ [K in keyof T]: T[K]["otype"]; }, undefined, "default">>]?: Pick<...>[K] | undefined; } & { [P in (keyof T & Exclude<...>) | ..... & {}, true>
  • Create ObjectSchema

    example

    { "type": "object", "properties": { ... }, "additionalProperties": false }

    Type parameters

    Parameters

    • props: T
    • Default value additional: boolean = false

    Returns ObjectSchema<{ 1: { [K in keyof { [K in keyof T]: T[K]["otype"]; }]?: { [K in keyof T]: T[K]["otype"]; }[K] | undefined; }; 0: Compute<{ [K in keyof Pick<{ [K in keyof T]: T[K]["otype"]; }, SelectKeys<{ [K in keyof T]: T[K]["otype"]; }, undefined, "default">>]?: Pick<...>[K] | undefined; } & { [P in (keyof T & Exclude<...>) | ..... & {}, true>

string

title

  • title(title: string): this

validate

  • validate(data: T): [boolean | PromiseLike<any>, ErrorObject[] | null | undefined]
  • Validate provided data with current schema using ajv, does not throw errors

    Parameters

    • data: T

    Returns [boolean | PromiseLike<any>, ErrorObject[] | null | undefined]

valueOf

  • valueOf(): S

Generated using TypeDoc