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

ng-custom-validator

v0.1.2

Published

Custom validation directive for AngularJS

Downloads

3

Readme

Custom validation directive for AngularJS

Build Status Coverage Status npm version

This is a generic validation directive to simplify custom validators for AngularJS written in TypeScript.

To create a custom validator you need to specify a single isValid() method. The wrapper takes care of setting the ngModel validity.

Installation

npm install ng-custom-validator --save

Usage

To create a custom validator, implement the IValidator interface and register it with the CustomValidator class.

export interface IValidator<TDependencies> {
  isValid<TValue>(value: TValue, attrs?: ng.IAttributes): boolean;
  setDependencies?(dependencies: TDependencies): void;
}

Implement this interface for your custom validator.

isValid<TValue>(value: TValue, attrs?: ng.IAttributes): boolean

Return the validity of the value parameter. This is where the custom validation logic goes. The attrs parameter holds the attributes on the <input /> tag the directive is attached to. This is useful when validating a value against some other value.

setDependencies?(dependencies: TDependencies): void;

Optional method for getting injected dependencies. If the validator needs access to an AngularJS service this is where they are injected.

class CustomValidator<TDependencies>

This is the class implementing the AngularJS specific details. Use it to register the directive with AngularJS. The constructor takes the a validator of type IValidator and a directiveName as paramaters and takes care of the rest. The directive name is used to set the form validity classes, e.g. if name is minValue the input will be marked with the CSS class ng-invalid-min-value by AngularJS.

Examples

See live examples by running npm start or head into the examples folder.

Minimum value validator

Create a class implementing the Validator<TDependencies> interface and register the directive with AngularJS.

import { IValidator } from 'ng-custom-validator';

interface IMinValueAttrs extends ng.IAttributes {
    minValue: string;
}

class MinValueValidator implements IValidator<void> {
    public isValid(value: number, attrs?: IMinValueAttrs ): boolean {
        if (!attrs.minValue) {
            return false;
        }

        return value >= parseInt(attrs.minValue, 10);
    }
}

const validator = new CustomValidator(MinValueValidator, 'minValue');
angular
    .module('app', [])
    .directive('minValue', validator.factory);

In the view the validator is used like any other directive:

<form name="form">
    <input ng-model="ctrl.minValue">
    <input name="value" ng-model="ctrl.value" min-value="{{ctrl.minValue}}"/>
    <div ng-if="form.value.$invalid">
        <p>Value should be above minimum value.</p>
    </div>
</form>

Validator using dependency injection

This example shows a date format validator with moment and a date format string injected by AngularJS.

import * as angular from 'angular';
import * as moment from 'moment';

import { IValidator } from 'ng-custom-validator';

interface IDateFormatDependencies {
  dateFormat: string;
}

class DateFormatValidator implements IValidator<IDateFormatDependencies> {
  private dateFormat: string;

  public isValid(value: string): boolean {
    return moment(value, this.dateFormat, true).isValid();
  }

  public setDependencies(deps: IDateFormatDependencies): void {
    this.dateFormat = deps.dateFormat;
  }
}
const dateValidValidator = new CustomValidator(DateFormatValidator, 'dateFormat');

angular
    .module('app', [])
    .constant('dateFormat', 'MM/DD/YYYY')
    .directive('dateFormat', (dateFormat: string) => dateValidValidator.factory({ dateFormat }));

In the view the validator is used like any other directive:

<form name="form">
    <input name="date" ng-model="ctrl.date" date-format />
    <div ng-if="form.date.$invalid">
        <p>Date is not in the right format.</p>
    </div>
</form>