$ yarn add ko-validate
or
$ npm install -S ko-validate
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
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.