New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mithril-validator

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mithril-validator

Easily validate Mithril.js forms, models, and objects.

latest
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

Mithril Validator

Easily validate Mithril.js models, and objects.

version License Build Status Downloads Code Climate Coverage Status Dependencies

Install

  • Download the latest package
  • NPM: npm install mithril-validator

Setup

var m = require('mithril')
require('mithril-validator')(m)

Documentation

new m.validator(validators) -> Validator

Validates mithril models and objects through validation functions mapped to specific model properties.

Example

// Our mithril model
var Todo = function (data) {
  this.name = m.prop(data.name || '')
  this.done = m.prop(data.done)
}

// Initialize a new validator
var validator = new m.validator({

  // Check model name property
  name: function (name) {
    if (!name) {
      return "Name is required."
    }
  }

})

// Results in "Name is required."
validator.validate(new Todo()).hasError('name')

validator.hasErrors() -> Boolean

Returns length of error mapping

if (validator.hasErrors()) {
  // do something
}

validator.hasError(key) -> Mixed

Returns the element associated with the specified key from the error mapping

m('input', {
  // Set class to error when an error for this field has occurred
  // Trigger validator on submission or when a field changes
  class: ctrl.validator.hasError('name') ? 'error' : '',
  onchange: m.withAttr('value', ctrl.model.name),
  value: ctrl.model.name()
})

validator.clearErrors() -> void

Removes all of the elements from the error list.

// Results in "Name is required."
validator.hasError('name')

validator.clearErrors()

// Results in undefined
validator.hasError('name')

validator.validate(model) -> Validator

Validates the specified model against the validations mapping in this instance.

Each (shallow) property is iterated over and cross-checked against the given model for value, then the validation function is invoked passing the model as context and value as the first argument.

On a truthy result from a validation function the result is placed on the error object with the property name as the identifier.

validator.validate(new Todo())

Note Missing properties are treated as undefined and do not throw errors, you should do this yourself within the property validator.

License

Licensed under The MIT License.

Keywords

mithril

FAQs

Package last updated on 05 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts