Skip to content

How to use prototype validator?

Van Noel edited this page Jul 15, 2019 · 6 revisions

Because the demonstration web are refactoring, I have no page to show you how to use this prototype, so I write the Wiki for temporary.

Installation

Using with NPM:

$ npm install --save @blacktoolbox/prototype-validator

// using ES6 modules
import Validator from '@blacktoolbox/prototype-validator';

// using CommonJS modules
var Validator= require('@blacktoolbox/prototype-validator');

Application

Event Rules

The validator is designed as a validator for 'event' which means it is not design for 'form'. So it do not bind with form and only triggered by event or function. For this, we need defined the event rule map as follow for example:

const rules = {
  'accChange' : [
    {
      rule    : 'length|min:2;max:6;',
      message : 'Length error',
      level   : 'error'
    }
  ],
  'pwdChange' : [
    {
      rule    : 'absLength|absLength:6',
      message : 'Password input error',
      level   : 'error'
    }
  ],
  'portChange' : [
    {
      rule    : 'range|min:1;max:65535',
      message : 'Port limited between 1-65535',
      level   : 'error'
    },
    {
      rule    : 'range|min:1;max:1023',
      message : 'It may have some conflict with some well known service, while using the port between 1-1023.',
      level   : 'warning'
    }
  ]
};

Every key name under rule are named with the concept of event, and they are also Array type which means we can have a list to make sure the values are actually we need. Every check rule can have their message and level. Cause the rule are defined in event angle. So we can execute when we need or different value validated with same rule depending on the user's design.

  • rule: Rule config included type and parameters.

  • message: Message for while fail.

  • level: Means the importance of the rule. For the situation when we do classify the level of rule.

Rule Type

Now we only support some rule.

  • 'type|type:number'

Check input's typeof

  • 'length|min:1;max:6'

Check the string's length between min and max. Otherwise, we can check min or max only like 'length|min:4'

  • 'absLength|absLength:7'

Check the string's absolute length like string.length === absLength

  • 'range|min:20;max:30'

Check the number's range between min and max. Otherwise, we can check min or max only like 'range|min:4'

  • 'absValue|absValue:1024'

Check the number's absolute value like number === absValue

  • 'format|type:email'

For now, we only support email format. It will support more usual format.

It is expected to have some rule usable.

Methods

Firstly we have to implement it, when we want to use the validator.

const Validator = new Validator();

And after defined the event-rule-map, we have some methods for using:

  • Validator.init();

    // To put our event-rule-map into the Validator to init it meanwhile create the status for each event rule.

  • Validator.status(event);

    // Status() means get all status, and status(event) means to get the status of event we want.

  • Validator.reset();

    // Reset the status of events.

  • Validator.validate();

    // Validate the value while event triggered.

Status

The structure of status for each event:

const structureStatus = function() {
  this.message = '';
  this.status = 'none';
};

We have two kept status:

  • 'none' are their initial state

  • 'pass' means the value pass all rule which means it may be the value we except.

Beta Version

Now we published is beta version. While it is completed enough, we will publish the formal version. It you have any problem or request please contact us. Thank you.

Clone this wiki locally