1.24.6 • Published 6 years ago

conforma v1.24.6

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Build Status conforma

Filter, validate and conform your data from POST request data in Express or other environment.

Install

npm install conforma --save

Usage

More examples you can find in example path or in test.

    var conforma = require('conforma');

    formData.setData({
      value1: '123',
      value2: 'yes',
      nested: {
        value1: 'email(at)localhost',
        value2: 'Hänsel und Gretel ',
        value4: 'SOME TRASH'
      },
      trashValue: 'foobar'
    }).default({
      value1: 1,
      value3: 'foobar',
      nested: {
        value3: '   <html>Hello World!</html>'
      }
    }).conform({
      value1: undefined,
      value2: undefined,
      nested: {
        value1: undefined,
        value2: undefined,
        value3: undefined
      }
    })
      .filter('value1', 'int')
      .filter('value2', 'bool')
      .filter('nested.value2', ['trim', 'toUpperCase'])
      .filter('nested.value3', ['string', 'trim', 'escapeHtml'])
      .validate('nested.value1', ['email', 'required'])
      .validate('nested.value2', {alpha: true})
      .exec(function(err, data) {
        err &&  console.log('Error: ', err);
        data && console.log('Data: ', data);
      });

If your data is invalid, then you get error:

    {
      [Error: Conforma Validation Error]
      errors:
       [ { name: 'ConformaError',
           message: 'email.invalid.format %s',
           field: 'nested.value1',
           value: 'email(at)localhost'
         },
         { name: 'ConformaError',
           message: 'only.alpha.allowed',
           field: 'nested.value2',
           value: 'HÄNSEL UND GRETEL'
         } ]
    }

If your data is valid, then you get the filtered and validated data:

    {
      value1: 123,
      value2: true,
      nested:
       { value1: 'email(at)localhost',
         value2: 'HÄNSEL UND GRETEL',
         value3: '&lt;html&gt;Hello World!&lt;/html&gt;'
       }
    }

Use with Promise (bluebird)

    var conforma = require('conforma');
    var formData = new conforma.Conforma();

    formData.setData({
      value1: '123',
      value2: 'yes'
    })
      .filter('value1', 'int')
      .filter('value2', 'bool')
      .exec()
      .then(function(data) {
        // ...
      })
      .catch(function(error) {
        // ...
      });

Use with your local filter/validator

    var conforma = require('conforma');
    var formData = new conforma.Conforma();
    
    formData.setData({
      value1: '123',
      value2: 'yes'
    })
      .filter('value1', function(value) {
          return Number(value);
        })
      .filter('value2', function(value) {
          return Boolean(value);
        })
      .exec()
      .then(function(data) {
        // ...
      })
      .catch(function(error) {
        // ...
      });

Use without new

    var conforma = require('conforma');

    conforma.Conforma({
      value1: '123',
      value2: 'yes'
    })
      .filter(...)
      .validate(...)
      .exec()
      .then(function(data) {
        // ...
      })
      .catch(function(error) {
        // ...
      });

Use in ES6 Style

    import {Conforma} from 'conforma';

    Conforma({
      value1: '123',
      value2: 'yes'
    })
      .filter(...)
      .validate(...)
      .exec()
      .then(function(data) {
        // ...
      })
      .catch(function(error) {
        // ...
      });

API

  • setData(object)
  • getData(filtered)
  • filter(path, filter, options)
  • validate(path, validator)
  • move(srcPath, destPath)
  • remove(path)
  • exec(callback) return Promise
  • reset
  • mount()

Filter

With Filter you can transform your data to your valid format

  • int
  • float
  • bool
  • digit
  • string
  • stringLength
  • trim whitespaces
  • lowerCase
  • upperCase
  • escapeHtml (<>"'& to entities)
  • addSlashes (like php)
  • stripHtml (sanitize all html content)
  • stripHtmlTags
  • email
  • url node url object keys
  • date moment format
  • object
  • array
  • null
  • uniqueList

Validator

  • required
  • empty allow empty fields
  • email
  • emailMx
  • alpha (UTF8 and whitespaces)
  • alnum (UTF8 and whitespaces)
  • number (allowed decimal place ,.)
  • notEmpty
  • equals
  • compare (with other fields)
  • contains
  • isDate (format please use moment)
  • inList
  • length (min, max)
  • objectId (Mongo ObjectID)

TODO

  • extend all tests
  • create some examples
  • extend validator/filter
  • extend README
  • your suggestion
  • i18n error messages
  • bug fixing
1.24.6

6 years ago

1.24.5

6 years ago

1.24.4

7 years ago

1.24.3

7 years ago

1.24.2

7 years ago

1.24.1

7 years ago

1.24.0

8 years ago

1.23.4

8 years ago

1.23.3

8 years ago

1.23.2

8 years ago

1.23.1

9 years ago

1.23.0

9 years ago

1.22.1

9 years ago

1.22.0

9 years ago

1.21.1

9 years ago

1.21.0

9 years ago

1.20.6

9 years ago

1.20.5

9 years ago

1.20.4

9 years ago

1.20.2

9 years ago

1.20.1

9 years ago

1.20.0

9 years ago

1.19.11

9 years ago

1.19.10

9 years ago

1.19.8

9 years ago

1.19.7

9 years ago

1.19.6

9 years ago

1.19.5

9 years ago

1.19.4

9 years ago

1.19.3

9 years ago

1.19.2

9 years ago

1.19.1

9 years ago

1.19.0

9 years ago

1.18.0

9 years ago

1.17.0

9 years ago

1.16.0

9 years ago

1.15.0

10 years ago

1.14.0

10 years ago

1.13.2

10 years ago

1.13.1

10 years ago

1.13.0

10 years ago

1.11.2

10 years ago

1.11.1

10 years ago

1.11.0

10 years ago

1.10.1

10 years ago

1.9.0

10 years ago

1.8.4

10 years ago

1.8.3

10 years ago

1.8.2

10 years ago

1.8.1

10 years ago

1.8.0

10 years ago

1.7.2

10 years ago

1.7.0

10 years ago

1.6.0

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.4

10 years ago

1.4.3

10 years ago

1.4.2

10 years ago

1.4.1

10 years ago

1.4.0

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago