type-definitions
The purpose of this package is to allow for defining type/object structure in an easily recognizable JSON format. An example definition might look something like:
; const myTypeDefinition =
It also offers validation of values based on those definitions:
; const aNumber = ; aNumber; // trueaNumber; // false const arrayOfStringsOrNumbers = ;arrayOfStringsOrNumbers; // truearrayOfStringsOrNumbers; // false const complexObject = ; complex; // true complex; // false (a.e !== Boolean)
Finally, it offers a way to construct objects of a given type and set defaults for that construction:
; const defaultString = ;const fooString = ;const barString = typesString; defaultString; // ''fooString // 'foo'barString // 'bar'barString // 'some other string'barString // throws error, invalid type const customObject = typesObject customObject // { a: '', b: 5, c: { d: false, e: true } }customObject; // { a: 'test', b: 5, c: { d: true, e: true } }
defineType
This function will process the input and return the representation as a class
with an isOfType
static function. It is primarily for coercing native
representations, and will pass through the package-defined types.
Therefore it's always good to wrap your definitions in this so that they
will support as much definition styles as possible.
Types
String
Validates true for string values (ie "test"
).
Default value: ""
Using package type:
; const aString = ;
This also supports coercion from native String
:
const aString = ;
Number
Validates true for number values (ie 1
, 1.23
, Infinity
, NaN
, 1.0003e+23
).
Default value: 0
Using package type:
; const aNumber = ;
This also supports coercion from native Number
:
const aNumber = ;
Boolean
Validates true for boolean values (ie true
, false
)
Default value: false
Using package type:
; const aBool = ;
This also supports coercion from native Boolean
:
const aBool = ;
Array
Validates true for any array (ie []
, ["test"]
, ["test", 2, true]
)
Default value: []
Using package type:
; const anArray = ;
This also supports coercion from native Array
or empty Array literal ([]
):
const anArray = ;const anotherArray = ;
Array.ofType(type)
Validates true for any array that matches type, including empty arrays (ie for types.Array.ofType(type.String)
, []
or ["test"]
)
You can also make an of Array of multiple types which will set the type to a Union of the types supplied
Default value: []
Using package type:
; const anArrayOfStrings = ;
This also supports coercion from an Array literal with types as members ([String]
, [String, Number]
):
const anArrayOfStrings = ;const anArrayOfStringsOrNumbers = ;
Object
Validates true for an object ({}
, new class Test {}
). Does not match null
or arrays.
Default value: {}
Using package type:
; const anObject = ;
This also supports coercion from native Object
and an empty object literal ({}
)""
const anObject = ;const anotherObject = ;
Object.withDefinition(objectDefinition)
Validates true for objects that match the defined structure. Note: it will
match objects with additional properties not included in the definition by
default. You can include the strict
Default value: default values of individual props
Using package type:
; const anObject = ;
This also supports coercion from an Object literal with members ({foo: String, bar: Number }
):
const anObject = ;
Object.withDefinition(objectDefinition).strict
Same as Object.withDefinition(objectDefinition)
except it also validates that
value does not have any additional keys
Default value: default values of individual props
Using package type:
; const anObject = ;
There is no native representation of this, however you can import the strict
function and use that to wrap native types:
; const aStringOrNull = ;
Object.keyValuePair(valueType)
Validates true for an object where all values match the supplied value type.
Default value: {}
Using package type:
; const aHashOfStrings = ;
There is no native representation for this
Any
Validates true for any value (allows null
but excludes undefined
).
Default value: null
Using package type:
; const anyType = ;
There is no native representation for this
Union.ofType(...types)
Validates true for any value matches any of the union types.
Default value: default value of first type
Using package type:
; const unionOfStringAndNumber = ;
There is no native representation of this, however you can import the unionOf
function and use that to wrap native types:
; const unionOfStringAndNumber =
Optional
All built in types include can be modified to validate for null/undefined by appending
the .optional
modifier
Default value: null
unless user specified
Using package type:
; const aStringOrNull = ;
There is no native representation of this, however you can import the optional
function and use that to wrap native types:
; const aStringOrNull =
Custom Classes
Custom classes can be supported by providing a static isOfType(val)
method on
your class. It will leverage whatever logic you put in this method for validation
Default value: Object.create(CustomClass)
static { return val__type === "myType" } { return "myType"; } { return "!!!"; } ; // false
If you use a class that does not define state isOfType(val)
then isOfType
will fall back to using instanceof
for validation
Type.withDefault() / Type.create()
Taking an exisitng type and calling .create(val)
on it will construct an instance of
of that type with the given value (assuming the provided value validates).
If no value is provided it will construct an object with the default value of an object (see defaults above)
If you would like to change the default value of an object there is a withDefault()
static on all
type extending from BaseType
(or coerced via defineType
) which takes a default value, validates
it against the type and if it's value uses that as the default construction value. Additionally
there is a defaultValue(type, val)
method exported for use in wrapping native types
For objects, you can provide a partial value to .create()
and it will use defaults to fill
in other properties as it is able to.