Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ObjectSchema<T, R>

Type parameters

  • T: Record<string, any>

  • R: boolean

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: Readonly<ObjectJsonSchema> = {} 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

additionalProperties

  • additionalProperties<P>(schema: P): P extends BaseSchema ? ObjectSchema<O.MergeUp<T, Record<string, P["otype"]>>, R> : this
  • This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself. Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties". For all such properties, validation succeeds if the child instance validates against the "additionalProperties" schema. Omitting this keyword has the same behavior as an empty schema.

    reference

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

    Type parameters

    Parameters

    • schema: P

    Returns P extends BaseSchema ? ObjectSchema<O.MergeUp<T, Record<string, P["otype"]>>, R> : this

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

dependencies

  • dependencies(deps: Record<string, string[] | BaseSchema>): this
  • This keyword specifies rules that are evaluated if the instance is an object and contains a certain property. This keyword's value MUST be an object. Each property specifies a dependency. Each dependency value MUST be an array or a valid JSON Schema. If the dependency value is a subschema, and the dependency key is a property in the instance, the entire instance must validate against the dependency value. If the dependency value is an array, each element in the array, if any, MUST be a string, and MUST be unique. If the dependency key is a property in the instance, each of the items in the dependency value must be a property that exists in the instance.

    reference

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

    Parameters

    Returns 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

maxProperties

  • maxProperties(maxProperties: number): this

minProperties

  • minProperties(minProperties: number): this

not

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

oneOf

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

optional

partial

patternProperties

  • patternProperties(props: Record<string, BaseSchema>): this
  • Each property name of this object SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect. Each property value of this object MUST be a valid JSON Schema. This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself. Validation of the primitive instance type against this keyword always succeeds. Validation succeeds if, for each instance name that matches any regular expressions that appear as a property name in this keyword's value, the child instance for that name successfully validates against each schema that corresponds to a matching regular expression.

    reference

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

    Parameters

    Returns this

prop

  • prop<K, S>(name: K, schema: S): this

propertyNames

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

required

  • required<S>(...fields: S): ObjectSchema<O.Required<T, S[number]>, R>

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

Generated using TypeDoc