npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-functional-validation

v0.1.2

Published

Yet another validation library for EmberJS, this one takes a much more functional approach

Downloads

7

Readme

Ember-Functional-Validation

An functional approach to object validations for emberjs.

This is just like Dockyard's ember validation library, except instead it builds a bunch of functions instead of being a bunch of object mixins that are advertised to work on all Ember.Objects, but actually only work on things with the container.

HowTo

Suppose you have a comment model Declare your validating function somewhere

# app/validators/comment
import FunctionalValidation from 'ember-functional-validation'

commentValidator = FunctionalValidation.create
  postName:
    presence: true
  opEmail:
    format: /\w+@\w+\.com/
    length:
      min: 5
  content:
    presence: true
export default commentValidator

Then, because commentValidator is just a function, use it wherever you please.

# app/controller/posts/new
import commentValidator from 'app/validators/comment'
PostsNewController = Ember.Controller.extend
  actions
    newComment: (comment) ->
      commentValidator comment
      .then (validComment) ->
        validComment.save()
        ...
      .catch (errors) ->
        @displayErrors errors

Note that the commentValidator always returns a promise

Because of the functional nature of your validator, you can compose them: (admittedly, although this feature is useful for building this library, I can't really think of an use-case for it in a real application)

import californianValidator from '../validators/californian'
import droughtVictimValidator from '../validators/drought-victim'
import compose from 'ember-functional-validation/utils/compose'

socalValidator = compose californianValidator, droughtVictomValidator
resident = store.createRecord "socalResident",
  lowPayingJob: true
  noWater: true
  traffic: true
  atLeastNotSF: true

socalValidator resident
.then (validResident) ->
  validResident.save()
  ...
.catch (errors) ->
  # do stuff with errors

Validators

The below is lifted almost bodily from dockyard's validation library:

https://raw.githubusercontent.com/dockyard/ember-validations/master/README.md

The sections not yet implemented are noted. (This is beta software, after all, and I'm writing this only because I actually need it, not because I'm trying to sell it to others)

Absence

Validates the property has a value that is null, undefined, or ''

Options

  • true - Passing just true will activate validation and use default message
  • message - Any string you wish to be the error message. Overrides i18n.
// Examples
absence: true
absence: { message: 'must be blank' }

Acceptance

not implemented yet By default the values '1', 1, and true are the acceptable values

Options

  • true - Passing just true will activate validation and use default message
  • message - Any string you wish to be the error message. Overrides i18n.
  • accept - the value for acceptance
// Examples
acceptance: true
acceptance: { message: 'you must accept', accept: 'yes' }

Confirmation

not implemented yet Expects a propertyConfirmation to have the same value as property. The validation must be applied to the property, not the propertyConfirmation (otherwise it would expect a propertyConfirmationConfirmation).

Options

  • true - Passing just true will activate validation and use default message
  • message - Any string you wish to be the error message. Overrides i18n.
// Examples
confirmation: true
confirmation: { message: 'you must confirm' }

Exclusion

not implemented yet A list of values that are not allowed

Options

  • message - Any string you wish to be the error message. Overrides i18n.
  • allowBlank - If true skips validation if value is empty
  • in - An array of values that are excluded
  • range - an array with the first element as the lower bound the and second element as the upper bound. Any value that falls within the range will be considered excluded
// Examples
exclusion: { in: ['Yellow', 'Black', 'Red'] }
exclusion: { range: [5, 10], allowBlank: true, message: 'cannot be between 5 and 10' }

Format

not implemented yet A regular expression to test with the value

Options

  • message - Any string you wish to be the error message. Overrides i18n.
  • allowBlank - If true skips validation if value is empty
  • with - The regular expression to test with
// Examples
format: { with: /^([a-zA-Z]|\d)+$/, allowBlank: true, message: 'must be letters and numbers only'  }

Inclusion

not implemented yet A list of the only values allowed

Options

  • message - Any string you wish to be the error message. Overrides i18n.
  • allowBlank - If true skips validation if value is empty
  • in - An array of values that are allowed
  • range - an array with the first element as the lower bound the and second element as the upper bound. Only values that fall within the range will be considered allowed
// Examples
inclusion: { in: ['Yellow', 'Black', 'Red'] }
inclusion: { range: [5, 10], allowBlank: true, message: 'must be between 5 and 10' }

Length

not implemented yet Define the lengths that are allowed

Options

  • number - Alias for is
  • array - Will expand to minimum and maximum. First element is the lower bound, second element is the upper bound.
  • allowBlank - If true skips validation if value is empty
  • minimum - The minimum length of the value allowed
  • maximum - The maximum length of the value allowed
  • is - The exact length of the value allowed
  • tokenizer - A function that should return a object that responds to length
Messages
  • tooShort - the message used when the minimum validation fails. Overrides i18n
  • tooLong - the message used when the maximum validation fails. Overrides i18n
  • wrongLength - the message used when the is validation fails. Overrides i18n
// Examples
length: 5
length: [3, 5]
length: { is: 10, allowBlank: true }
length: { minimum: 3, maximum: 5, messages: { tooShort: 'should be more than 3 characters', tooLong: 'should be less than 5 characters' } }
length: { is: 5, tokenizer: function(value) { return value.split(''); } }

Numericality

not implemented yet Will ensure the value is a number

Options

  • true - Passing just true will activate validation and use default message
  • allowBlank - If true skips validation if value is empty
  • onlyInteger - Will only allow integers
  • greaterThan - Ensures the value is greater than
  • greaterThanOrEqualTo - Ensures the value is greater than or equal to
  • equalTo - Ensures the value is equal to
  • lessThan - Ensures the value is less than
  • lessThanOrEqualTo - Ensures the value is less than or equal to
  • odd - Ensures the value is odd
  • even - Ensures the value is even
Messages
  • greaterThan - Message used when value failes to be greater than. Overrides i18n
  • greaterThanOrEqualTo - Message used when value failes to be greater than or equal to. Overrides i18n
  • equalTo - Message used when value failes to be equal to. Overrides i18n
  • lessThan - Message used when value failes to be less than. Overrides i18n
  • lessThanOrEqualTo - Message used when value failes to be less than or equal to. Overrides i18n
  • odd - Message used when value failes to be odd. Overrides i18n
  • even - Message used when value failes to be even. Overrides i18n
// Examples
numericality: true
numericality: { odd: true, messages: { odd: 'must be an odd number' } }
numericality: { onlyInteger: true, greaterThan: 5, lessThanOrEqualTo : 10 }

Presence

Validates the property has a value that is not null, undefined, or ''

Options

  • true - Passing just true will activate validation and use default message
  • message - Any string you wish to be the error message. Overrides i18n.
// Examples
presence: true
presence: { message: 'must not be blank' }

Uniqueness

Not yet implemented.

Conditional Validators

not implemented yet Each validator can take an if or an unless in its options hash. The value of the conditional can be an inline function, a string that represents a property on the object, or a string that represents a function on the object. The result should be a boolean.

note that if is considered a keyword in IE8 and so you should put it in quotes

// function form
firstName: {
  presence: {
    'if': function(object, validator) {
      return true;
    }
  }
}

// string form
// if 'canValidate' is a function on the object it will be called
// if 'canValidate' is a property object.get('canValidate') will be called
firstName: {
  presence: {
    unless: 'canValidate'
  }
}

Custom Validators

Custom (or inline) validators take an arbitrary function which allow you to decide how to validate your model.

Have your custom function return null or a promise resolving to a null if validation passes.

have your function return a string or a promise to a string if validation fails.

email:
  inline: (model) ->
    store.find "user", Ember.get(model, "email")
    .then (user) ->
      return if user.get("isMasterrace")
      "not master-race"
    .catch ->
      "cannot be found"

Installation

  • git clone this repository
  • npm install
  • bower install

Running

  • ember server
  • Visit your app at http://localhost:4200.

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.