awkward-validator

Custom validation module for Oscar and Owl UI components

Usage no npm install needed!

<script type="module">
  import awkwardValidator from 'https://cdn.skypack.dev/awkward-validator';
</script>

README

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