Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ISchemaField<T>

Configures a field in the schema. Cosmosnaut uses JSON Schema as a standard, cross-language way to validate data. This object is a JSON 7 Schema which is validated by Ajv. The Ajv instance can be found as a static property on BaseModel.ajv, which you can use to register custom validation functions as needed.

Once you define validation on the schema field, you can get a JSON Schema for the full model by calling schema.jsonSchema. Models can be individually validated by calling BaseModel.validate, and they're validated automatically when calling save, create, or update.

Additionally, you can pass a Transform in the transform property here. Validation is run on the non-transformed, database version of the properties.

Here's an example using all of these features:

const userSchema = createSchema('users')
  .partitionKey('/id')
  // Do some basic length validation on the username:
  .field('username', asType<string>(), {
    type: 'string',
    maxLength: 20,
    minLength: 2,
  })
  // Validate `favoriteColors`. Store it as an array in Cosmos DB, but
  // use it as a Set in the model.
  .field('favoriteColors', asType<Set<string>>(), {
    type: 'array',
    maxItems: 3,
    items: { type: 'string' },
    transform: new Transform<string[], Set<string>>(
      stored => new Set(stored),
      app => Array.from(app),
    ),
  });

Type parameters

  • T

Hierarchy

  • JSONSchema7
    • ISchemaField

Index

Properties

Optional $comment

$comment: string

Optional $id

$id: string

Optional $ref

$ref: string

Optional $schema

$schema: string

Optional additionalItems

additionalItems: JSONSchema7Definition

Optional additionalProperties

additionalProperties: JSONSchema7Definition

Optional allOf

allOf: JSONSchema7Definition[]

Optional anyOf

anyOf: JSONSchema7Definition[]

Optional const

const: JSONSchema7Type

Optional contains

contains: JSONSchema7

Optional contentEncoding

contentEncoding: string

Optional contentMediaType

contentMediaType: string

Optional default

default: JSONSchema7Type

Optional definitions

definitions: {}

Type declaration

  • [key: string]: JSONSchema7Definition

Optional dependencies

dependencies: {}

Type declaration

  • [key: string]: JSONSchema7Definition | string[]

Optional description

description: string

Optional else

else: JSONSchema7Definition

Optional enum

enum: JSONSchema7Type[]

Optional examples

examples: JSONSchema7Type

Optional exclusiveMaximum

exclusiveMaximum: number

Optional exclusiveMinimum

exclusiveMinimum: number

Optional format

format: string

Optional if

if: JSONSchema7Definition

Optional isRequired

isRequired: boolean

Optional items

items: JSONSchema7Definition | JSONSchema7Definition[]

Optional maxItems

maxItems: number

Optional maxLength

maxLength: number

Optional maxProperties

maxProperties: number

Optional maximum

maximum: number

Optional minItems

minItems: number

Optional minLength

minLength: number

Optional minProperties

minProperties: number

Optional minimum

minimum: number

Optional multipleOf

multipleOf: number

Optional not

not: JSONSchema7Definition

Optional oneOf

oneOf: JSONSchema7Definition[]

Optional pattern

pattern: string

Optional patternProperties

patternProperties: {}

Type declaration

  • [key: string]: JSONSchema7Definition

Optional properties

properties: {}

Type declaration

  • [key: string]: JSONSchema7Definition

Optional propertyNames

propertyNames: JSONSchema7Definition

Optional readOnly

readOnly: boolean

Optional required

required: string[]

Optional then

then: JSONSchema7Definition

Optional title

title: string

Optional transform

transform: Transform<any, T>

Optional type

type: JSONSchema7TypeName | JSONSchema7TypeName[]

Optional uniqueItems

uniqueItems: boolean

Optional writeOnly

writeOnly: boolean

Generated using TypeDoc