Validation

Weave has a built-in validation module to validation action parameters.

Valdiation example

The validator is activated by default. All parameters to be validated must be defined in the params object in the action definition.

    const { Weave } = require('@weave-js/core')

    // Create broker
    const broker = Weave({
        validate: true // default value is 'true'
    })

    // Create a service
    broker.createService({
        name: 'user',
        actions: {
            sayHello: {
                params: {
                    name: { type: 'string', min: 4 }
                },
                handler(ctx) {
                    
                }
            }
        }
    })

    broker.start()
        .then(() => {
            // No cached result found, so call the handler.
            return broker.call('order.list')
                .then(result => broker.log.info('Orders found:', result.length))
        })
        .then(() => {
            // Cached result found. Return the cached result.
            return broker.call("user.list"
                ).then(result => broker.log.info('Cached orders found:', res.length))
        })

Built-in validators

any

any does not validate the type of the given value.

string

This is a string validator. The type of value must be String.

Properties

Name Type Default Description
minLength Integer null Minimal length of the value.
maxLength Integer null Maximum length of the value.
equal String null String must equal the given value.
contains String null The value must contain this string.

array

This is a array validator. The type of value must be Array.

Properties

Name Type Default Description
contains Object null Validator object to validate the array values.

boolean

This is a boolean validator. The type of value must be boolean.

date

This is a date validator. The type of value must be date.

email

This is a email validator. The given value must be a valid email address.

Properties

Name Type Default Description
mode String basic Check method. precise, basic

forbidden

This valdiator throws an error if this property exists in the object.

number

This is a number validator. The type of value must be Number.

Properties

Name Type Default Description
minLength Integer null Minimal length of the value.
maxLength Integer null Maximum length of the value.
equal String null String must equal the given value.
contains String null The value must contain this string.

object

This is a object validator. The type of value must be Object.

url

This is a email validator. The given value must be a valid URL.