Model

Model

Factory class to generate Model instances against their JSON Schema definitions. This class adheres to the Flux Standard Action (FSA) specification and generates FSA representations of itself when transfromed to JSON. Therefore, it expects data to be provided in the same structure. The Model class implements a similar pattern to Object property descriptors, allowing you to set whether the instance's properties are configurable, enumerable, and/or writable. We have added another descriptor, immutable, to allow for the creation of immutable instances of a Model.

NOTE: Third argument in the constructor can be either a JSON Schema refs object or a descriptors object if refs aren't required.

Constructor

(async) new Model(data, schema, refsopt, descriptorsopt)

Parameters:
Name Type Attributes Default Description
data Object A FSA Object literal containing the Model's type and optional meta and payload.
schema Object | Schema An Object literal or Schema instance containing the JSON Schema definition of the Model.
refs Object <optional>
An optional Object literal of cached JSON Schema definitions referenced in the main schema.
descriptors Object <optional>
{ configurable: true, enumerable: true, writable: true } An optional Object literal containing the desired property descriptors for the Model instance.

Methods

(static) errors(model) → {Array.<string>}

Static method to get the List of errors from the last time validate was called on the associated JSON Schema instance from the Model instance.
Parameters:
Name Type Description
model Model The Model instance.
Returns:
Type:
Array.<string>
The List of errors.

(static) schema(model) → {Schema}

Static method to get the JSON Schema instance associated with the instance of a Model.
Parameters:
Name Type Description
model Model The Model instance.
Returns:
Type:
Schema
- The associated JSON Schema instance.

(static) validate(model, schemaopt) → {Boolean}

Static method to manually run the JSON Schema instance's validate method on the specified Model instance. This method will also perform partial schema validation if passed as the second parameter.
Parameters:
Name Type Attributes Description
model Model The Model instance.
schema Schema <optional>
An optional JSON Schema instance.
Returns:
Type:
Boolean
true if validation is successful, otherwise false.

(static) version(model, versionopt) → {Number|undefined}

Static method to get the version associated with the instance of a Model.
Parameters:
Name Type Attributes Description
model Model The Model instance.
version Number <optional>
An optional version Number to set
Returns:
Type:
Number | undefined
- The version of the Model instance.

toJSON() → {Object}

Method used to transform the Model instance to conform to the FSA specification standard action definition.
Returns:
Type:
Object
An Object literal containing the meta and optional data of the Model instance.