String fields
Schema.string
Basic example
Let's suppose we're modelling information about a dinosaur.
(For now, we'll just store its species as a string, e.g. 'Diplodocus'
.)
Creation
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const dinosaurSchema = {
species: Schema.string
}
class Dinosaur extends ActiveClass(dinosaurSchema) {}
new Dinosaur() // ActiveClassError: Could not construct Dinosaur. The required property 'species' is missing
new Dinosaur({}) // ActiveClassError: Could not construct Dinosaur. The required property 'species' is missing
new Dinosaur({ species: true }) // ActiveClassError: Could not construct Dinosaur. The property 'species' is of the wrong type
new Dinosaur({ species: null }) // ActiveClassError: Could not construct Dinosaur. The property 'species' is of the wrong type
new Dinosaur({ species: 'Diplodocus', randomProp: 'Triceratops' }) // works (but randomProp gets ignored as it is not on the schema)
new Dinosaur({ species: 'Diplodocus' }) // works
new Dinosaur({ species: 'Triceratops' }) // works
Assignment
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const dinosaurSchema = {
species: Schema.string
}
class Dinosaur extends ActiveClass(dinosaurSchema) {}
const dinosaur = new Dinosaur({ species: 'Diplodocus' })
dinosaur.species = 'T-Rex' // works
dinosaur.species = 7 // ActiveClassError: Dinosaur could not accept the value 7 (number) at path 'species'. The property 'species' is of the wrong type
dinosaur.species = undefined // ActiveClassError: Dinosaur could not accept the value undefined (undefined) at path 'species'. The required property 'species' is missing
dinosaur.species = null // ActiveClassError: Dinosaur could not accept the value null (object) at path 'species'. The property 'species' 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 dinosaur schema to demonstrate:
roar
should default to'RAWR'
;name
, should be an optional property;home
, should default to'Earth'
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 dinosaurSchema = {
species: Schema.string,
roar: Schema.string({ default: 'RAWR' }),
name: Schema.string({ optional: true }), // or required: false,
home: Schema.string({ optional: true, default: 'Earth' })
}
class Dinosaur extends ActiveClass(dinosaurSchema) {}
const dinosaur = new Dinosaur({ species: 'Diplodocus' })
dinosaur.species // => 'Diplodocus'
dinosaur.roar // => 'RAWR'
dinosaur.name // => undefined
dinosaur.home // => 'Earth'
/* species: required and no default */
dinosaur.species = undefined // ActiveClassError: Dinosaur could not accept the value undefined (undefined) at path 'species'. The required property 'species' is missing
dinosaur.species = null // ActiveClassError: Dinosaur could not accept the value null (object) at path 'species'. The property 'species' is of the wrong type
/* roar: required and has default */
dinosaur.roar = undefined
dinosaur.roar // => 'RAWR' (default kicks in when undefined)
dinosaur.roar = null // ActiveClassError: Dinosaur could not accept the value null (object) at path 'roar'. The property 'roar' is of the wrong type
/* name: optional and has no default */
dinosaur.name = undefined
dinosaur.name // => undefined (optional, so doesn't throw, and has no default to kick in)
dinosaur.name = null
dinosaur.name // => null (optional, so doesn't throw and can be null)
/* home: optional and has default */
dinosaur.home = undefined
dinosaur.home // => 'Earth' (default kicks in when undefined)
dinosaur.home = null
dinosaur.home // => null (optional, so doesn't throw and can be null)