Number fields
Schema.number
Basic example
Let's suppose we're modelling a building, which has a number of floors.
Creation
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const buildingSchema = {
floors: Schema.number
}
class Building extends ActiveClass(buildingSchema) {}
new Building() // ActiveClassError: Could not construct Building. The required property 'floors' is missing
new Building({}) // ActiveClassError: Could not construct Building. The required property 'floors' is missing
new Building({ floors: '4' }) // ActiveClassError: Could not construct Building. The property 'floors' is of the wrong type
new Building({ floors: null }) // ActiveClassError: Could not construct Building. The property 'floors' is of the wrong type
new Building({ floors: 4, randomProp: 9 }) // works (but randomProp gets ignored as it is not on the schema)
new Building({ floors: 4 }) // works
new Building({ floors: 9 }) // works
Assignment
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const buildingSchema = {
floors: Schema.number
}
class Building extends ActiveClass(buildingSchema) {}
const building = new Building({ floors: 4 })
building.floors = 5 // works
building.floors = '5' // ActiveClassError: Building could not accept the value "5" (string) at path 'floors'. The property 'floors' is of the wrong type
building.floors = undefined // ActiveClassError: Building could not accept the value undefined (undefined) at path 'floors'. The required property 'floors' is missing
building.floors = null // ActiveClassError: Building could not accept the value null (object) at path 'floors'. The property 'floors' is of the wrong type
Configuration
A schema property can have a default value, and/or be optional.
In either case, if the property has a default value and/or is optional, it does not need to be supplied when the ActiveDocument is created.
Default values are used when a field's value would otherwise be undefined
.
Only optional properties can be assigned null
(i.e. the deliberate ommission of a value).
Let's add some additionl properties to our building schema to demonstrate:
doors
should default to1
;rooms
, should be an optional property;chimneys
, should default to2
and be an optional property.
- Runtime type checks (JS)
- Static / compile-time type checks (TS)
Assignment when | default defined | no default defined |
---|---|---|
required: true (default) or optional: false | null : throws undefined : uses default | null : throws undefined : throws |
optional: true or required: false | null : uses null undefined : uses default | null : uses null undefined : uses undefined |
import { ActiveClass, Schema } from 'fireactive'
const buildingSchema = {
floors: Schema.number,
doors: Schema.number({ default: 1 }),
rooms: Schema.number({ optional: true }), // or required: false,
chimneys: Schema.number({ optional: true, default: 2 })
}
class Building extends ActiveClass(buildingSchema) {}
const building = new Building({ floors: 4 })
building.floors // => 4
building.doors // => 1
building.rooms // => undefined
building.chimneys // => 2
/* floors: required and no default */
building.floors = undefined // ActiveClassError: Building could not accept the value undefined (undefined) at path 'floors'. The required property 'floors' is missing
building.floors = null // ActiveClassError: Building could not accept the value null (object) at path 'floors'. The property 'floors' is of the wrong type
/* doors: required and has default */
building.doors = undefined
building.doors // => false (default kicks in when undefined)
building.doors = null // ActiveClassError: Building could not accept the value null (object) at path 'doors'. The property 'doors' is of the wrong type
/* rooms: optional and has no default */
building.rooms = undefined
building.rooms // => undefined (optional, so doesn't throw, and has no default to kick in)
building.rooms = null
building.rooms // => null (optional, so doesn't throw and can be null)
/* chimneys: optional and has default */
building.chimneys = undefined
building.chimneys // => false (default kicks in when undefined)
building.chimneys = null
building.chimneys // => null (optional, so doesn't throw and can be null)