No description, website, or topics provided.
JavaScript OCaml
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib errors returned from validate-form are real error objects Sep 18, 2013
test
.gitignore
.travis.yml
LICENCE initial Jun 21, 2013
README.md fix git links Dec 6, 2013
credit-card.js updated validate-form so that all errors have a type property Jul 11, 2013
docs.mli
email.js updated validate-form so that all errors have a type property Jul 11, 2013
equal.js
hash.js
index.js added index.js that exports ALL THE THINGS Dec 6, 2013
list.js list should handle function & array Sep 19, 2013
match.js
max.js number comparison should fail on NaN value Jul 18, 2013
member-of.js
min.js
optional.js
package.json
range.js number comparison should fail on NaN value Jul 18, 2013
size.js rename length.js to size.js Jul 22, 2013
truthy.js updated validate-form so that all errors have a type property Jul 11, 2013
type.js implemented type Jul 11, 2013
validate-if.js Added validate-if validator Sep 23, 2013

README.md

validate-form

build status NPM version Coverage Status David Dependency Status

browser support

Simple functional form validation

Example

var Validator = require("validate-form")
var truthy = require("validate-form/truthy")
var isEmail = require("validate-form/email")
var isCreditCard = require("validate-form/credit-card")
var range = require("validate-form/range")
var match = require("validate-form/match")
var memberOf = require("validate-form/member-of")
var list = require("validate-form/list")

var validDate = /^\d\d\d\d\/\d\d$/
var countries = ["US-en", "UK-en", "BR-pt", ...]
var validate = Validator({
  firstName: [truthy()],
  lastName: [truthy("Custom message: The %s field is required")],
  email: [truthy(), isEmail("Please ensure that you enter valid email")],
  cardNumber: [isCreditCard()],
  cvv: [range(3, 4)],
  expirationDate: [match(validDate)],
  country: [memberOf(countries, "enter valid country code")],
  interest: [list({
    min: 3,
    content: [truthy()]
  })]
})

Creating your own validators

You can use custom functions as validators. A validator function takes the value to validate as an argument, the key for that value and the parent object that the value is on.

You can use the key to make more readable validation errors and you can use the parent to do validation logic across multiple properties

A validator should either return nothing or an error or an array of errors, an error in this case is { message: String, type: String }. The type is useful if you want to show custom error messages in the UI, then you can ignore the message and use a custom error message for each type of validation error.

var Validator = require("validate-form")

var validate = Validator({
  name: [function isValidName(value, key, parent) {
    var message = ""
    if (typeof value !== "string") {
      message = key + " should be a string"
    } else if (value.length < 4) {
      message = key + " should be at least 4 characters"
    }

    if (message) {
      return { message: message, type: "invalidName" }
    }
  }]
})

Docs

type AlmostValidateError := {
    type: ValidateErrorType, message: String
}
type PossibleValidateError = Array<AlmostValidateError> |
    AlmostValidateError | null

type Validator := (value: Any, key: String, parent: Object) =>
    PossibleValidateError
type ValidateErrorType := "creditCard" | "email" | "length" |
  "match" | "max" | "memberOf" | "min" | "range" | "truthy" | "type"
type ValidateError := {
  attribute: String,
  message: String,
  type: ValidateErrorType
}

validate-form := (Object<String, Array<Validator>>) =>
    Array<ValidationError> | null

validate-form/add-error := (errors: Array<ValidationError>, key: String,
    maybeError: PossibleValidateError) => Array<ValidationError>

Installation

npm install validate-form

Contributors

  • Raynos

MIT Licenced