1.24.6 • Published 5 years ago

conforma v1.24.6

Weekly downloads
3
License
MIT
Repository
github
Last release
5 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

5 years ago

1.24.5

5 years ago

1.24.4

6 years ago

1.24.3

6 years ago

1.24.2

6 years ago

1.24.1

6 years ago

1.24.0

7 years ago

1.23.4

7 years ago

1.23.3

7 years ago

1.23.2

7 years ago

1.23.1

7 years ago

1.23.0

7 years ago

1.22.1

7 years ago

1.22.0

7 years ago

1.21.1

8 years ago

1.21.0

8 years ago

1.20.6

8 years ago

1.20.5

8 years ago

1.20.4

8 years ago

1.20.2

8 years ago

1.20.1

8 years ago

1.20.0

8 years ago

1.19.11

8 years ago

1.19.10

8 years ago

1.19.8

8 years ago

1.19.7

8 years ago

1.19.6

8 years ago

1.19.5

8 years ago

1.19.4

8 years ago

1.19.3

8 years ago

1.19.2

8 years ago

1.19.1

8 years ago

1.19.0

8 years ago

1.18.0

8 years ago

1.17.0

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.0

8 years ago

1.13.2

8 years ago

1.13.1

9 years ago

1.13.0

9 years ago

1.11.2

9 years ago

1.11.1

9 years ago

1.11.0

9 years ago

1.10.1

9 years ago

1.9.0

9 years ago

1.8.4

9 years ago

1.8.3

9 years ago

1.8.2

9 years ago

1.8.1

9 years ago

1.8.0

9 years ago

1.7.2

9 years ago

1.7.0

9 years ago

1.6.0

9 years ago

1.5.1

9 years ago

1.5.0

9 years ago

1.4.4

9 years ago

1.4.3

9 years ago

1.4.2

9 years ago

1.4.1

9 years ago

1.4.0

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago