awkward-validator

1.3.0 • Public • Published

#Awkward Validator

Oscar and Owl's custom (albeit limited) form validation tool. If any changes are made, please ensure full code coverage.

This module modifies the array of objects given in the data parameter. data.property.error can be used to display input errors to the user.

var av = require('awkward-validator');

var data = [{
      name: {
        value: '',
        placeholder: '',
        error: 'Please enter a different name',
        valid: true
      },
      mobile_number: {
        value: '',
        placeholder: '',
        error: 'Invalid phone number',
        valid: true
      },
      email_address: {
        value: '',
        placeholder: '',
        error: 'Invalid email address',
        valid: true
      }
    }];

    var schema = {
      type: 'array',
      items: {
        properties: {
          name: {
            properties: {
              value: {
                type: 'string',
                minLength: 2,
                maxLength: 50
              }
            }
          },
          mobile_number: {
            properties: {
              value: {
                type: 'string',
                minLength: 11,
                maxLength: 15
              }
            }
          },
          email_address: {
            properties: {
              value: {
                type: 'string',
                pattern: '^\\S+@\\S+$',
              }
            }
          }
        }
      }
    }

    var result = av.validate(data, schema);

    if(!result) {
      throw new Error();
    }

    // Submit form

Readme

Keywords

none

Package Sidebar

Install

npm i awkward-validator

Weekly Downloads

0

Version

1.3.0

License

UNLICENSED

Last publish

Collaborators

  • oscarandowl