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-cli-validatorjs

v0.2.0

Published

Validate your Ember Objects.

Downloads

5

Readme

Ember-cli-validatorjs

This addon is a wrapper around ValidatorJS that let's you drop validation into any component, controller, model, or Ember Object.

Installation

  • ember install ember-cli-validatorjs

Use

Import the validatable mixin:

import Validatable from 'ember-cli-validatorjs/mixins/validatable';

export default Ember.Object.extend(Validatable, {
  // Your logic here
});

Then declare your validation rules with the rules hash on your object:


export default Ember.Object.extend(Validatable, {
  rules: {
    email: 'required|email',
    password: 'required'
  }
});

To run validations, use the validate function and pass in data to be validated:

export default Ember.Object.extend(Validatable, {
  rules: {
    email: 'required|email',
    password: 'required'
  },

  actions: {
    submitForm: function () {
      var formData = {};
      // Logic to set properties from form
      if (this.validate(formData)) {
        alert('success');
      } else {
        alert('failure');
      }
    }
  }
});

When a validation fails, your object will now have a validationErrors property set to the validation errors from validatorJs.

You can also set custom messages for your validation errors by setting the validationMessages property on your object. Follow the validatorJs documentation for declaring these messages.

One More Thing

When working with controllers, it's pretty common to need to validate the properties for the current model object. Since Ember uses the get function to be able to grab proxied and computed properties, this makes it hard for validatorjs to validate models.

But, if you create a validatedProperties array on your object and run validate without passing in data, then those properties will be grabbed from your model.

export default Ember.Controller.extend(Validatable, {
  validatedProperties: [
    'email',
    'password'
  ],

  rules: {
    email: 'required|email',
    password: 'required'
  },

  actions: {
    checkUser: function () {
      // Logic to set properties from form
      if (this.validate()) {
        alert('success');
      } else {
        alert('failure');
      }
    }
  }
});

If you want to hack this a bit more you can create your own getValidatedProperties function which is called whenever no data is passed to the validate function.