API Reference

PerfectSchema

Usage

import PerfectSchema from '@perfect-schema/base';

const schema = new PerfectSchema({
  foo: type%,
  bar: {
    type: type%,
    // ...
  }
});

Definitions


:large_blue_diamond: static method use(pluginFactory)

Make all schema use the given plugin created by the specified plugin factory, a function returning either a function or an object.

Arguments
Returns
Example
function pluginFactory1(PerfectSchema) {
  return (schema) => { /* ... */ };
};

function pluginFactory2(PerfectSchema) {
  return {
    preInit: (schema, fields, options) => { /* ... */ },
    init: (schema) => { /* ... */ },
    extendModel: (model, schema) => { /* ... */ },
    extendContext: (context, schema) => { /* ... */ }
  };
};

:small_orange_diamond: property fields

The normalized fields specification.


:small_orange_diamond: property fieldNames

The array consisting of the list of specified field names.


:small_orange_diamond: property options

The options passed to the constructor, or an empty object if no option was provided. Modifying the options after the instance has been created may yield unpredictible results.


:new: constructor PerfectSchema(fields, options)

Create a new schema given the specified fields. Options are used only by plugins.

Arguments

:large_orange_diamond: method createContext(name)

Create a new validation context based on the given schema.

Arguments
Returns

:large_orange_diamond: method createModel(data)

Create a new model based on the schema’s specifications. The returned model is a POJO object containing the required fields, or fields with defined default values.

Arguments
Returns
Example
const user = userSchema.createModel({
  username: 'john.doe',
  password: '53cr37'
});
// { username: 'john.doe', password: '53cr37', active: true, createdAt: ... }</code></pre>

ValidationContext

Usage

const context = schema.createContext();

Definitions


:new: constructor ValidationContext(schema)

Create a new schema validator.

Arguments

:large_orange_diamond: method getMessage(field)

Return the validation message for the given field. If the field is valid, the method returns undefined.

Arguments
Returns

:large_orange_diamond: method getMessages()

Return a shallow copy of the validation messages.

Returns

:large_orange_diamond: method isValid()

Is the last current validation context valid?

Returns

:large_orange_diamond: method reset()

Reset all messages from the validation context, and set it as valid.


:large_orange_diamond: method setMessage(field, message)

Set a validation message for the specified field. The field name must exist in the schema. The method can also be used to set sub-schema validation messages. All messages are reset automatically, including sub-schema messages, when the context is revalidated or reset.

Arguments
Example
context.setMessage('foo', 'message');
context.setMessage('foo.bar', 'another');
context.getMessages();
// { 'foo': 'message', 'foo.bar': 'another' }
context.getMessage('foo');
// 'message'

:large_orange_diamond: method validate(data, options)

Validate the given value against the validation context’s specified schema. The options will be passed to each validation functions. The function returns whether the context is valid or not after the validation.

Arguments
Example
context.validate({
  foo: 'Hello',
  bar: true
});
// true