Code Monkey home page Code Monkey logo

a6y-format-and-validate's Introduction

A6Y - Format and validate

Format and validate form's fields values.

Quick start

  1. Install:
$ yarn add @altalogy/a6y-format-and-validate
  1. Import:
import * as fv from '@altalogy/a6y-format-and-validate'

// Or:

import { formatDate } from '@altalogy/a6y-format-and-validate'
  1. Use:
fv.formatDate('2000-12-30 14:30', 'dd-MM-yyyy')

// Or:

formatDate('2000-12-30 14:30', 'dd-MM-yyyy')

// Output: Sa-30-2000

Validator usage

The validate functions takes two params:

  • values - map of values: { 'field-name': 'field-value' },
  • fields - configuration containing validation rules.

The fields configuration contains:

  • name - the field name (the same as 'field-name'),
  • rules - array of constraints that the field's values has to satisfy. Each rule contains a type and may contain additional fields.

Examples:

// Values from the form:
const values = {
  title: 'My title',
  description: 'Some description',
  price: 12.99
}

// The validation config defining fields constraints:
const fields = [

  // The 'title' field:
  {
    name: 'title', // The field name
    rules: [
      {
        type: 'CANNOT_BE_BLANK' // The rule type
      },
      {
        type: 'MAX_LENGTH', // The rule type
        limit: 100 // Some rules may require additional data, like the length limit in MAX_LENGTH case
      }
    ]
  }

  // The 'description' field
  {
    name: 'title', // The field name
    rules: [
      {
        type: 'MAX_LENGTH',
        limit: 255
      }
    ]
  }

]

The validate() function returns:

import { validate } from '@altalogy/a6y-format-and-validate'


const values = { ... } // form values
const fields = [ ... ] // the fields configuration

validate(values, fields)

// Return:

{
  isValid: false, // false if any of values is invalid. True if all are valid.
  errors: {
    fieldName: [ 'CANNOT_BE_BLANK' ], // the array of invalid rules for the 'fieldName' field.
    title: [ 'MAX_LENGTH' ]
  }
}

Available cases:

// CANNOT_BE_BLANK
{
  type: 'CANNOT_BE_BLANK',
}


// MAX_LENGTH
{
  type: 'MAX_LENGTH',
  limit: 100
}

Formater usage

Format Date

Formats date using moment.js:

import { formatDate } from '@altalogy/a6y-format-and-validate'

// formatDate(value, format)
formatDate('2000-12-30 14:30', 'dd-MM-yyyy')

Development

  1. Add a new function to validate or format value in: ./src

  2. Add JSDoc comment what the function does.

  3. Add test in __tests__

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.