API Reference
PerfectSchema
Usage
import PerfectSchema from '@perfect-schema/base';
const schema = new PerfectSchema({
foo: type%,
bar: {
type: type%,
// ...
}
});
Definitions
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
-
pluginFactory :
function
A function returning a plugin.
Returns
- Nothing.
Example
function pluginFactory1(PerfectSchema) {
return (schema) => { /* ... */ };
};
function pluginFactory2(PerfectSchema) {
return {
preInit: (schema, fields, options) => { /* ... */ },
init: (schema) => { /* ... */ },
extendModel: (model, schema) => { /* ... */ },
extendContext: (context, schema) => { /* ... */ }
};
};
property fields
The normalized fields specification.
property fieldNames
The array
consisting of the list of specified field names.
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.
constructor PerfectSchema(fields, options)
Create a new schema given the specified fields. Options are used only by plugins.
Arguments
-
fields :
Object
An object of field specifications.
-
options :
Object
(Optional)Options passed to the schema as required by plugins, etc.
method createContext(name)
Create a new validation context based on the given schema.
Arguments
-
name :
String
(Optional)The name of the context to return
Returns
ValidationContext
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
-
data :
Object
The default data to set for the model. This data will be extended if necessary with the schema fields’ default values. No validation is performed on this data.
Returns
Object
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
constructor ValidationContext(schema)
Create a new schema validator.
Arguments
-
schema :
PerfectSchema
The schema to create a validator for.
method getMessage(field)
Return the validation message for the given field. If the field is valid, the method returns undefined.
Arguments
-
field :
String
The field name to return the message for.
Returns
-
String
undefined
method getMessages()
Return a shallow copy of the validation messages.
Returns
Object
method isValid()
Is the last current validation context valid?
Returns
Boolean
method reset()
Reset all messages from the validation context, and set it as valid.
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
-
field :
String
The field name as string.
-
message :
String
(Optional)The message as string, or any falsy value to reset the field.
Example
context.setMessage('foo', 'message');
context.setMessage('foo.bar', 'another');
context.getMessages();
// { 'foo': 'message', 'foo.bar': 'another' }
context.getMessage('foo');
// 'message'
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
-
data :
mixed
The data to validate.
-
options :
Object
(Optional)The validation options
-
options.fields :
Array
(Optional)The list of fields to validate. Will validate all fileds from the schema if not specified.
-
options.validatorOptions :
Object
(Optional)An object that will be passed to the validators. Defaults to an empty object.
-
options.fields :
Example
context.validate({
foo: 'Hello',
bar: true
});
// true