Giter VIP home page Giter VIP logo

validate-form's Introduction

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

validate-form's People

Contributors

matt-esch avatar raynos avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.