# Class: ReferenceType

The Reference type creates a relationship between two schemas.

# Options

  • required flag to define if the field is mandatory
  • validator that will be applied to the field a validation function, validation object or string with the name of the custom validator
  • default that will define the initial value of the field, this option allows a value or a function
  • immutable that will define this field as immutable. Ottoman prevents you from changing immutable fields if the schema as configure like strict

example

const Cat = model('Cat', {name: String});
const schema = new Schema({
    type: String,
    isActive: Boolean,
    name: String,
    cats: [{ type: CatSchema, ref: 'Cat' }],
  });
const User = model('User', schema);
1
2
3
4
5
6
7
8

# Hierarchy

CoreType

ReferenceType

# Constructors

# constructor

+ new ReferenceType(name: string, schema: Schema, refModel: string, options?: CoreTypeOptions): ReferenceType

Overrides CoreType.constructor

Parameters:

Name Type
name string
schema Schema
refModel string
options? CoreTypeOptions

Returns: ReferenceType

# Properties

# name

name: string

Inherited from IOttomanType.name


# Optional options

options? : CoreTypeOptions

Inherited from CoreType.options


# refModel

refModel: string


# schema

schema: Schema


# typeName

typeName: string

Inherited from IOttomanType.typeName


# Static sName

sName: string = "Reference"

Overrides CoreType.sName

# Accessors

# default

get default(): unknown

Inherited from CoreType.default

Returns: unknown


# required

get required(): boolean | RequiredOption | RequiredFunction

Inherited from CoreType.required

Returns: boolean | RequiredOption | RequiredFunction


# validator

get validator(): ValidatorOption | ValidatorFunction | string | undefined

Inherited from CoreType.validator

Returns: ValidatorOption | ValidatorFunction | string | undefined

# Methods

# buildDefault

buildDefault(): unknown

Inherited from CoreType.buildDefault

Returns: unknown


# cast

cast(value: any): any

Overrides IOttomanType.cast

Parameters:

Name Type
value any

Returns: any


# checkRequired

checkRequired(): string | void

Inherited from CoreType.checkRequired

Returns: string | void


# checkValidator

checkValidator(value: unknown): void

Inherited from CoreType.checkValidator

Parameters:

Name Type
value unknown

Returns: void


# isEmpty

isEmpty(value: unknown): boolean

Inherited from CoreType.isEmpty

Parameters:

Name Type
value unknown

Returns: boolean


# isStrictStrategy

isStrictStrategy(strategy: VALIDATION_STRATEGY): boolean

Inherited from CoreType.isStrictStrategy

Parameters:

Name Type
strategy VALIDATION_STRATEGY

Returns: boolean


# validate

validate(value: unknown, strategy: any): any

Overrides CoreType.validate

Parameters:

Name Type
value unknown
strategy any

Returns: any