/ko-validate

:checkered_flag: Declarative Validation for KnockoutJS

Primary LanguageJavaScriptDo What The F*ck You Want To Public LicenseWTFPL

ko-validate

NPM License Travis Coveralls DavidDM

Getting Started

Install

$ yarn add ko-validate

or

$ npm install -S ko-validate

Usage

import validate from 'ko-validate'

const foos = {
  foo: undefined,
  bar: {
    baz: undefined
  }
}

const validationRules = {
  foo: {
    required: true
  },
  bar: {
    baz: {
      required: true,
      number: true
    }
  }
}

validate(foos, validationRules)

foos.isValid() // false
foos.foo.isValid() // false
foos.bar.isValid() // false
foos.bar.baz.isValid() // false

foos.foo('foo')

foos.isValid() // false
foos.foo.isValid() // true
foos.bar.isValid() // false
foos.bar.baz.isValid() // false

foos.bar.baz(1)

foos.isValid() // true
foos.foo.isValid() // true
foos.bar.isValid() // true
foos.bar.baz.isValid() // true

foos.foo(undefined)

foos.isValid() // false
foos.foo.isValid() // false
foos.bar.isValid() // true
foos.bar.baz.isValid() // true

What About Validation Messages?

Write your own. Make your error messages meaningful. Default/automatic validation messages feel robotic and often a) lack context, b) use jargon your users don't understand. Custom bindings and components are easy to create for consistent validation messages across your app.