Boolean fields
Schema.boolean
Basic example
Let's suppose we're modelling a lightbulb, which can either be on or off.
Creation
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const lightbulbSchema = {
isOn: Schema.boolean
}
class Lightbulb extends ActiveClass(lightbulbSchema) {}
new Lightbulb() // ActiveClassError: Could not construct Lightbulb. The required property 'isOn' is missing
new Lightbulb({}) // ActiveClassError: Could not construct Lightbulb. The required property 'isOn' is missing
new Lightbulb({ isOn: 'yes' }) // ActiveClassError: Could not construct Lightbulb. The property 'isOn' is of the wrong type
new Lightbulb({ isOn: null }) // ActiveClassError: Could not construct Lightbulb. The property 'isOn' is of the wrong type
new Lightbulb({ isOn: true, randomProp: true }) // works (but randomProp gets ignored as it is not on the schema)
new Lightbulb({ isOn: true }) // works
new Lightbulb({ isOn: false }) // works
Assignment
- Run-time type checks (JS)
- Static / compilation type checks (TS)
import { ActiveClass, Schema } from 'fireactive'
const lightbulbSchema = {
isOn: Schema.boolean
}
class Lightbulb extends ActiveClass(lightbulbSchema) {}
const lightbulb = new Lightbulb({ isOn: true })
lightbulb.isOn = false // works
lightbulb.isOn = 'true' // ActiveClassError: Lightbulb could not accept the value "true" (string) at path 'isOn'. The property 'isOn' is of the wrong type
lightbulb.isOn = undefined // ActiveClassError: Lightbulb could not accept the value undefined (undefined) at path 'isOn'. The required property 'isOn' is missing
lightbulb.isOn = null // ActiveClassError: Lightbulb could not accept the value null (object) at path 'isOn'. The property 'isOn' 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 lightbulb schema to demonstrate:
isEco
should default tofalse
;isLED
, should be an optional property;isSmart
, should default tofalse
and be an optional property.
- Run-time 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 lightbulbSchema = {
isOn: Schema.boolean,
isEco: Schema.boolean({ default: false }),
isLED: Schema.boolean({ optional: true }), // or required: false,
isSmart: Schema.boolean({ optional: true, default: false })
}
class Lightbulb extends ActiveClass(lightbulbSchema) {}
const lightbulb = new Lightbulb({ isOn: false })
lightbulb.isOn // => false
lightbulb.isEco // => false
lightbulb.isLED // => undefined
lightbulb.isSmart // => false
/* isOn: required and no default */
lightbulb.isOn = undefined // ActiveClassError: Lightbulb could not accept the value undefined (undefined) at path 'isOn'. The required property 'isOn' is missing
lightbulb.isOn = null // ActiveClassError: Lightbulb could not accept the value null (object) at path 'isOn'. The property 'isOn' is of the wrong type
/* isEco: required and has default */
lightbulb.isEco = undefined
lightbulb.isEco // => false (default kicks in when undefined)
lightbulb.isEco = null // ActiveClassError: Lightbulb could not accept the value null (object) at path 'isEco'. The property 'isEco' is of the wrong type
/* isLED: optional and has no default */
lightbulb.isLED = undefined
lightbulb.isLED // => undefined (optional, so doesn't throw, and has no default to kick in)
lightbulb.isLED = null
lightbulb.isLED // => null (optional, so doesn't throw and can be null)
/* isSmart: optional and has default */
lightbulb.isSmart = undefined
lightbulb.isSmart // => false (default kicks in when undefined)
lightbulb.isSmart = null
lightbulb.isSmart // => null (optional, so doesn't throw and can be null)