Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseSchema<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>

anyOf

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

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

  • This validation outcome of this keyword's subschema has no direct effect on the overall validation result. Rather, it controls which of the "then" or "else" keywords are evaluated. When "if" is present, and the instance successfully validates against its subschema, then validation succeeds against this keyword if the instance also successfully validates against this keyword's subschema.

    reference

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

    Parameters

    Returns this

ifThenElse

not

  • not<P>(not: P): this
  • It MUST be a valid JSON Schema.

    example

    { not: {} }

    Type parameters

    Parameters

    • not: P

    Returns this

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

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
  • It returns all the schema values

    Returns S

Generated using TypeDoc