Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ArraySchema<T, R>

Type parameters

  • T

  • 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<ArrayJsonSchema> = {} 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

additionalItems

  • additionalItems<P>(additional: P): P extends BaseSchema ? ArraySchema<T | P["type"], 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

contains

  • contains<P>(contains: P): ArraySchema<T | P["type"], R>

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

items

  • This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema. If "items" is an array of schemas, validation succeeds if each element of the instance validates against the schema at the same position, if any. Omitting this keyword has the same behavior as an empty schema.

    reference

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

    Type parameters

    Parameters

    • items: P | P[]

    Returns ArraySchema<P["type"], R>

maxItems

  • maxItems(maxItems: number): this

minItems

  • minItems(minItems: number): this

not

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

oneOf

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

optional

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

uniqueItems

  • uniqueItems(unique?: boolean): 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