Options
All
  • Public
  • Public/Protected
  • All
Menu

Class StringSchema<T, R>

Type parameters

  • T: string

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

contentEncoding

  • contentEncoding(contentEncoding: string): this

contentMediaType

  • contentMediaType(contentMediaType: string): this

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

format

  • format(format: string): this

id

  • id($id: string): this

ifThen

ifThenElse

maxLength

  • maxLength(maxLength: number): this

minLength

  • minLength(minLength: number): this

not

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

oneOf

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

optional

pattern

  • pattern(pattern: RegExp): this

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

Generated using TypeDoc