<commit_before>Express Form provides data filtering and validation as route middleware to your Express applications.

Usage:
------

    var form = require("express-form"),
        filter = form.filter,
        validate = form.validate;

    var app = express.createServer();

    app.configure(function() {
      app.use(express.bodyDecoder());
      app.use(app.router);
    });

    app.post(

      // Route
      '/user',
      
      // Form filter and validation middleware
      form(
        filter("username").trim(),
        validate("username").required().is(/^[a-z]+$/),
        filter("password").trim(),
        validate("password").required().is(/^[0-9]+$/)
      ),
      
      // Express request-handler now receives filtered and validated data
      function(req, res){
        if (!req.form.isValid) {
          // Handle errors
          console.log(req.form.errors);

        } else {
          // Or, use filtered form data from the form object:
          console.log("Username:", req.form.username);
          console.log("Password:", req.form.password);
        }
      }
    );

Documentation:
--------------

### Module

The Express Form **module** returns an Express [Route Middleware](http://expressjs.com/guide.html#Route-Middleware) function. You specify filtering and validation by passing filters and validators as arguments to the main module function. For example:

    var form = require("express-form");

    app.post('/user',
      
      // Express Form Route Middleware: trims whitespace off of
      // the `username` field.
      form(form.filter("username").trim()),
      
      // standard Express handler
      function(req, res) {
        // ...
      }
    );


### Filters

The `filter` property of the module creates a filter object tied to a specific field.

    filter(fieldname);
    // -> Filter

The API is chainable, so you can keep calling filter methods one after the other:

    filter("username").trim().toLower().truncate(5)


#### Filter API:

Type Coercion

    toFloat()           -> Number

    toInt()             -> Number, rounded down

    toBoolean()         -> Boolean from truthy and falsy values

    toBooleanStrict()   -> Only true, "true", 1 and "1" are `true`

    ifNull(replacement) -> "", undefined and null get replaced by `replacement`
    

HTML Encoding for `& " < >`

    entityEncode() -> encodes HTML entities

    entityDecode() -> decodes HTML entities 


String Transformations

    trim(chars)                 -> `chars` defaults to whitespace

    ltrim(chars)

    rtrim(chars)

    toLower() / toLowerCase()

    toUpper() / toUpperCase()

    truncate(length)            -> Chops value at (length - 3), appends `...`
    

Custom Filters

    custom(function)
    
        Filters the field value using custom logic.
    
        Example:
        If the `name` field has a value of "hello there", this would
        transform it to "hello-there". 

        filter("name").custom(function(value) {
          return value.replace(/\s+/g, "-");
        });
    

### Validators

The `validate` property of the module creates a validator object tied to a specific field.

    validate(fieldname[, label]);
    // -> Validator

The API is chainable, so you can keep calling validator methods one after the other:

    validate("username").required().isAlphanumeric()


#### Validator API:

**Validation messages**: each validator has its own default validation message. These can easily be overridden at runtime by passing a custom validation message to the validator. The custom message is always the **last** argument passed to the validator.

Use "%s" in the message to have the field name or label printed in the message:

    validate("username").required()
    // -> "Missing field"
    
    validate("username").required("%s is a required field.")
    // -> "username is a required field."
    
    validate("username", "Username").required("%s is a required field.")
    // -> "Username is a required field."


**Validation Methods**
<commit_msg>Updated examples<commit_after>Express Form provides data filtering and validation as route middleware to your Express applications.

Usage:
------

    var form = require("express-form"),
        filter = form.filter,
        validate = form.validate;

    var app = express.createServer();

    app.configure(function() {
      app.use(express.bodyDecoder());
      app.use(app.router);
    });

    app.post(

      // Route
      '/user',
      
      // Form filter and validation middleware
      form(
        filter("username").trim(),
        validate("username").required().is(/^[a-z]+$/),
        filter("password").trim(),
        validate("password").required().is(/^[0-9]+$/)
      ),
      
      // Express request-handler now receives filtered and validated data
      function(req, res){
        if (!req.form.isValid) {
          // Handle errors
          console.log(req.form.errors);

        } else {
          // Or, use filtered form data from the form object:
          console.log("Username:", req.form.username);
          console.log("Password:", req.form.password);
        }
      }
    );

Documentation:
--------------

### Module

The Express Form **module** returns an Express [Route Middleware](http://expressjs.com/guide.html#Route-Middleware) function. You specify filtering and validation by passing filters and validators as arguments to the main module function. For example:

    var form = require("express-form");

    app.post('/user',
      
      // Express Form Route Middleware: trims whitespace off of
      // the `username` field.
      form(form.filter("username").trim()),
      
      // standard Express handler
      function(req, res) {
        // ...
      }
    );


### Filters

The `filter` property of the module creates a filter object tied to a specific field.

    filter(fieldname);
    // -> Filter

The API is chainable, so you can keep calling filter methods one after the other:

    filter("username").trim().toLower().truncate(5)


#### Filter API:

Type Coercion

    toFloat()           -> Number

    toInt()             -> Number, rounded down

    toBoolean()         -> Boolean from truthy and falsy values

    toBooleanStrict()   -> Only true, "true", 1 and "1" are `true`

    ifNull(replacement) -> "", undefined and null get replaced by `replacement`
    

HTML Encoding for `& " < >`

    entityEncode() -> encodes HTML entities

    entityDecode() -> decodes HTML entities 


String Transformations

    trim(chars)                 -> `chars` defaults to whitespace

    ltrim(chars)

    rtrim(chars)

    toLower() / toLowerCase()

    toUpper() / toUpperCase()

    truncate(length)            -> Chops value at (length - 3), appends `...`
    

Custom Filters

    custom(function)
    
        Filters the field value using custom logic.
    
        Example:
        If the `name` field has a value of "hello there", this would
        transform it to "hello-there". 

        filter("name").custom(function(value) {
          return value.replace(/\s+/g, "-");
        });
    

### Validators

The `validate` property of the module creates a validator object tied to a specific field.

    validate(fieldname[, label]);
    // -> Validator

The API is chainable, so you can keep calling validator methods one after the other:

    validate("username").required().isAlphanumeric()


#### Validator API:

**Validation messages**: each validator has its own default validation message. These can easily be overridden at runtime by passing a custom validation message to the validator. The custom message is always the **last** argument passed to the validator.

Use "%s" in the message to have the field name or label printed in the message:

    validate("username").required()
    // -> "username is required"
    
    validate("username").required("What is your %s?")
    // -> "What is your username?"
    
    validate("username", "Username").required("What is your %s?")
    // -> "What is your Username?"


**Validation Methods**

{'fraction_non_alphanumeric': 0.1251238850346878, 'fraction_numerical': 0.0014866204162537165, 'mean_word_length': 2.4280254777070063, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3885302', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2089, 'n_words': 734}