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-validation-state

v1.0.1

Published

Utilizes ember-validators to provide a decorator for form validation state.

Downloads

132

Readme

ember-validation-state

CI Ember Observer Score

An Octane-ready decorator which provides form-field validation state by utilizing ember-validators validators.

This addon takes heavy inspiration, and is based mostly upon, the work in ember-cp-validations, but takes the reactive decorator approach rather than the Mixin approach.

Compatibility

  • Ember.js v3.12 or above
  • Ember CLI v2.13 or above
  • Node.js v10 or above

Installation

ember install ember-validation-state

Usage

import Component from '@glimmer/component';
import validationState, { validate } from 'ember-validation-state';

const Validators = {
  username: [validate('presence', { presence: true })],
  password: [validate('length', { min: 6 })]
};

class MyForm extends Component {
  @tracked username = null;
  @tracked password = null;

  @validationState(Validators) validationState;
}
<input value={{this.username}} />

{{#unless this.formValidState.attrs.username.isValid}}
  {{#each this.formValidState.attrs.username.messages as |msg|}}
    <p>{{msg}}</p>
  {{/each}}
{{/unless}}

You can also pass a "thunk" to the validationState decorator, for lazy initialization of your Validators:

import Component from '@glimmer/component';
import validationState, { validate } from 'ember-validation-state';

const Validators = {
  username: [validate('presence', { presence: true })],
  password: [validate('length', { min: 6 })]
};

class MyForm extends Component {
  @tracked username = null;
  @tracked password = null;

  @validationState((component) => component.limitedValidators) validationState;

  get limitedValidators() {
    if (!this.args.username) {
      return {
        password: Validators['password']
      };
    }
  }
}

ValidationState definition

Please refer to the types in index.d.ts for full typescript type definitions.

Intl

By default, if ember-intl is installed, validationState will attempt to look for a message for a specific validation error in your translations file. If no key is present, it will fall back to the ember-validators message.

# en-us.yml

errors:
  # provide intl version of of ember-validators `blank`
  blank: '{description} cannot be blank'

intlKey

Pass intlKey if you would like to use a different intl key. Will be prefixed with errors. for the translations file lookup

# en-us.yml

errors:
  username-empty: 'Gotta fill in username'
import { validate } from 'ember-validation-state';

const Validators = {
  username: [validate('presence', { presence: true, intlKey: 'username-empty' })]
};

descriptionKey

Pass descriptionKey if you would like to internationalize the "description" of the field. Default is "This field". Will be prefixed with errors. for the translations file lookup

# en-us.yml

errors:
  usernames: 'Username'

  # message that `descriptionKey` lookup will be inserted into
  blank: '{description} cannot be blank'
import { validate } from 'ember-validation-state';

const Validators = {
  username: [validate('presence', { presence: true, descriptionKey: 'usernames' })]
};

Custom validation methods

Custom validation methods can be passed in the array for a specific key. They are passed along the Messages builder for convenience.

Validator signature

interface MessageBuilder {
  getMessageFor(type: string, context: object): string
}

type Validator = (value: any, messages: MessageBuilder) => [boolean, string];

In action:

# en-us.yml

errors:
  password-regex: 'Password does not match required format'
import Component from '@glimmer/component';
import validationState, { validate } from 'ember-validation-state';

function passwordRegex(value, messages) {
  return [
    /W/.test(value),
    messages.getMessageFor('password-regex')
  ];
}

const Validators = {
  username: [validate('presence', { presence: true })],
  password: [
    validate('length', { min: 6 }),
    passwordRegex
  ]
};

class MyForm extends Component {
  @tracked username = null;
  @tracked password = null;

  @validationState(Validators) validationState;
}

Usage with Typescript

This package, although not yet rewritten in Typescript, is fully compatible and exports its own types.

To have full typings support of the property initialized by validationState, utilize the ValidationState type:

import validationState, {
  validate,
  ValidationState,
} from 'ember-validation-state';

const AttrValidators = {
  name: [validate('presence', { presence: true })],
  description: [validate('presence', { presence: true })],
};

export default class {
  @validationState(AttrValidators)
  declare formValidState: ValidationState<typeof AttrValidators>;
}

Utilizing the generic argument typeof AttrValidators provides autocomplete for the formValidState.attrs hash.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.