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-lgtm

v0.2.0

Published

Ember CLI addon for Square's LGTM validation library

Downloads

28

Readme

Build Status

Ember-lgtm

This ember-cli addon provides a Mixin around Square's LGTM Validation library. The mixin follows recommended validation patterns. This addon also adds a few LGTM helpers to add support for nested validation and registers LGTM to use Ember's RSVP

Installation in your app

ember install ember-lgtm

Usage

In your component or model class, where you will add your validation, simply include the Validatable Mixin and provide a validator object. See the example below:

import LGTM from 'lgtm';
import Validatable from 'ember-lgtm/mixins/validatable';

Ember.Component.extend(Validatable, {
    validator: LGTM.validator()
        .validates('email')     // Name of your property
        .required('Email address is required')
        .email('Enter a valid email')
        .build(),
    actions: {
        save() {
            this.validate().then(isValid => {
                if (isValid) {
                    // Perform the actual save
                }
            })
        }
    }
    // Other code...
});

Then in your template, you have access to an errors array for each validated property.

<label>Email:</label>
{{input value=email class=(if errors.email "error")}}
<span class="error">{{errors.email}}</span>
<button onclick={{action "save"}}>Save</button>

Behavior

When the properties are in an valid state (initial), the validation won't be triggered. This leads to a better UX since the user doesn't want to see errors while he's in the middle of typing something that will likely turn something from invalid to valid. However, if a property is already in an invalid state, it will be re-validated when the property changes. This is also known as inline validation and is useful to provide immediate feedback when somethings goes from invalid to valid.

Nested validation

ember-lgtm also adds two LGTM helpers to add support for nested validations for objects and arrays.

isValid

Validates a validatable object. A validatable object is anything with a validate function returning a boolean promise (like the mixin mixin/validatable)

In this example, the property billingAddress contains an address object that implements a validate function to validate the individual properties of the address. The parent object only checks the overall validity of the object, rather than each individual field.

validator: LGTM.validator()
    .validates('fullName')
        .required('Full name is required')
    .validates('billingAddress')
        .isValid()
    .build()

allAreValid

Validates all validatable objects in the array are valid. A validatable object is anything with a validate function returning a boolean promise (like the mixin mixin/validatable).

This functions like isValid, except that the validated property should be an array of validatable objects, which must all be valid in order for this validator function to return true.

validator: LGTM.validator()
    .validates('arrayOfAddresses')
        .allAreValid()
    .build()

You can create your own Custom Helpers by using LGTM.helpers.register directly.

Server side validation

LGTM uses Promises and this addon configures it to use the same RSVP library that Ember uses. When doing server side validation, the validate function returns a Promise and each validation registered either return immediately or return a promise. LGTM will execute all the promises in parallel and wait for all the validation logic to complete before returning the validation information.

LGTM

LGTM is a simple JavaScript library for validating objects and collecting error messages. It leaves the display, behavior, and error messages in your hands, focusing on letting you describe your validations cleanly and concisely for whatever environment you're using.

You can find more about it on the LGTM Wiki that includes an API Reference and information about how to create Custom Helpers

Samples

Running samples locally

  • git clone https://github.com/practicefusion/ember-lgtm
  • yarn
  • ember server
  • Visit your app at http://localhost:4200 will show the demo app.

Running tests

  • ember test
  • ember test --server

More

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