schema

schema

Source:

The schema part of using models for data.

Methods

(static) createDefault(schema) → {object}

Source:
Create a schema instance with all values initialised to their default values.
Parameters:
Name Type Description
schema * The schema to instantiate
Returns:
A schema-conformant object with all properties set to their default values.
Type
object

(static) createValidator(schema, strictopt, allowIncompleteopt) → {function}

Source:
A convenience function for creating a validator that can be passed around, bound to objects, etc.

  import { schema } from "use-models-for-data";

  Object.defineProperty(mySchema, `validate`, {
    enumerable: false,
    value: schema.createValidator(mySchema, false)
  });
Parameters:
Name Type Attributes Description
schema schema The schema against which to validate data objects.
strict boolean <optional>
True to perform strict validation, or false to validate with coercion. Defaults to true.
allowIncomplete boolean <optional>
True to allow missing required fields. Defaults to false.
Returns:
a function with the same signature as schema.validate.
Type
function

(static) getModelSet(Model) → {Array.<Model>}

Source:
See:
Similar to unlinkSchema, without rewriting the parent when a child model is found. Instead, we get a list of all distinct Model classes.
Parameters:
Name Type Description
Model Model A fully qualified Model.
Returns:
- An array consisting of all individual models found by walking through the passed model.
Type
Array.<Model>

(static) getRecordNameFor(schema, instance) → {String}

Source:
Get the string identifier for this schema-conformant data object.
Parameters:
Name Type Description
schema * A schema definition
instance * A schema-conformant data object
Returns:
the string identifier for this schema-conformant data object.
Type
String

(static) linkSchema(schemaInstance, getLatestSchema) → {schema}

Source:
Fully quality a schema by linking in all external schema it depends on.
Parameters:
Name Type Description
schemaInstance * A schema instance with sub-schema link information.
getLatestSchema * A resolver function for finding sub-schema definitions.
Returns:
The schema instance with all links replaced by fully qualified sub-schema.
Type
schema

(static) migrate(object, operations)

Source:
Migrate an object from being schema1-conformant to schema2-conformant.
Parameters:
Name Type Description
object object The object to migrate
operations operations A sequence of operational transforms (e.g. a "diff").
Returns:
An object that conforms to the updated schema.

(static) migrate^2(object, schema1, schema2)

Source:
Parameters:
Name Type Description
object object The object to migrate
schema1 schema The "original" schema from which to migrate our object.
schema2 schema The "current" schema to which we want to migrate our object.
Returns:
An object that conforms to the updated schema.

(static) unlinkSchema(schema) → {Array.<schema>}

Source:
Decompose a single schema into a set of linked schema, based on the __meta.distinct property of modelfields with a .shape
Parameters:
Name Type Description
schema * A fully qualified schema
Returns:
An array consisting of all individual schema found by walking through the passed schema.
Type
Array.<schema>

(static) validate(schema, object, strict, allowIncomplete) → {object}

Source:

Validate an object against a schema. This function does not return a boolean, but a result object that takes the following form:


  {
    passed: boolean,
    warnings: string[],
    errors: string[],
  }

As such, use this function in your own code as:


  const strict = trueOrFalse;
  const allowIncomplete = trueOrFalse;
  const result = validate(someSchema, myObject, strict, allowIncomplete);
  if (result.passed) {
    // all good, although result.warnings may contain strict/incomplete related warnings
  } else {
    // tap into result.errors to find out why your object is invalid
  }

Note that the strict flag determines whether or not to perform coercive validation. If strict=false and the code does need to perform coercion in order for validation to pass, the object will be updated such that it will now pass strict validation.

Parameters:
Name Type Description
schema schema The schema that the object should conform to
object object The object to check conformance for
strict boolean True if strict type validation is required, false for coercive
allowIncomplete boolean Do not fail validation if there are fields missing from this object
Returns:
See description above
Type
object