Default Validator

The Prudence validator can be accessed by simply calling Prudence() with its arguments.

Prudence(object, schema[, errorMessages[, options]])

Validated the provided object against the provided schema.

Arguments
  • object (object) – The object to validate.

  • schema (object) – The schema to validate against.

  • errorMessages (object) – Error messages to use on error, defaults to {}.

  • options (object) – Options for the Prudence validator. See Options.

Returns

A string error message on failure, and null on success.

Warning

Prudence returns null on success. To keep this clear, you should use Prudence like this:

let err = Prudence(/* ... */);

if (err) { /* do error handling */ }

Instanced Validator

In the case that you want to always use a specific set of options while parsing, you can instantiate your own validator with the following command.

let myPrudenceValidator = new Prudence.Validator(myPreferedOptions);

// Note: you can still pass an options param, all this accomplishes is changing the validator defaults.
let err = myPrudenceValidator.validate(object, schema, errorMessages, options);

Error Message Overrides

The third argument to Prudence is errorMessages, and it expects an object with identical structure to the schema, containing error messages.

This can be used to override existing error messages, or to provide them for functions (Although the alternative of Validation Functions is prefered).

let schema = {
    username: "string"
}

let errMsgs = {
    username: "Custom Error Message Here"
}

// err is "[username] Custom Error Message Here. Received null." - Note the automatic prefixing and suffixing of information.
let err = Prudence({ username: null }, schema, errMsgs);

Options

The below table indicates the options available for Prudence. The defaults are in brackets.

Key

Type (Default)

Description

allowExcessKeys

boolean (false)

Whether or whether not to allow excess keys on the provided object, such as { foo, bar } being validated by schema { foo }.

throwOnNonObject

boolean (true)

Whether or whether not to throw an error if a non-object is provided as the object parameter (such as accidentally sending undefined).

Default Options

The default options for Prudence can be accessed at Prudence.Validator.defaultOptions.