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

ngx-model-validator

v1.1.1

Published

A policy based angular model fluid validation framework

Downloads

16

Readme

ngx-validation-framework

A policy based fluid model validation framework for Angular projects.

npm version CircleCI

Demo

https://ngx-model-validator.web.app/simple

Installation

npm i ngx-model-validator --save

Usage

  • Simple validation with error display on field
<ngx-component-validator [model]="person" [policy]="PERSON_POLICY_NAME" [policyFnRef]="PERSON_VALIDATION_FN"></ngx-component-validator>

<input matInput placeholder="First Name" ngxMatValidate [model]="person" validateProperty="firstName" [policy]="PERSON_POLICY_NAME" [(ngModel)]="person.firstName" />

import NgxValidationModule in app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { NgxValidationModule } from 'ngx-model-validator';

import { AppComponent } from './app.component';

@NgModule({
    imports: [BrowserModule, FormsModule, NgxValidationModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}
// policy

import { NgxValidatorHelper , NgxValidationPolicy } from 'ngx-model-validator';

export class PersonValidationPolicy implements NgxValidationPolicy {

    addValidations(validatorHelper: NgxValidatorHelper): any[] {

        const lastNameValidatorFn = (model, ...args) => {
            
            if(model.firstName && model.firstName.length > 1) {
                return true;
            } else {
                return false;
            }
        }

        const cityAndStateAsyncValidatorFn = async(errorMsg:string, val :any, policy:any, model:any)=>{

            // api key get resets everyday - get new one while testing
            const api_key = "api_key";
            const url = `some_url/${api_key}`;
            const result = await (await fetch(url)).json();

            if(result){
                if(result.state === model.address.state) {
                    return true;
                }
            } 
            
            policy.errorMsg = errorMsg
            
        }


        const personValidations = [
            validatorHelper.validateFor('firstName').isRequired('First Name is required'),
            validatorHelper.validateFor('lastName', lastNameValidatorFn).isRequired('Last Name is required'),
            validatorHelper.validateFor('age').isRequired('Age is required').isNumber('Age should be number')
                .isNumberWithinRange('Age should be between 10 and 60', [10,60]),
            validatorHelper.validateFor('gender').isRequired('Gender is required'),
            validatorHelper.validateFor('email').isRequired('Email is required').isRegex('Enter valid email', 
            /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),
            validatorHelper.validateFor('address.line1').isRequired('Street address is required'),
            validatorHelper.validateFor('address.city','address.line1.length > 0').isRequired('City name is required'),
            validatorHelper.validateFor('address.state','address.line1.length > 0').isRequired('State name is required'),
            validatorHelper.validateFor('address.zip','address.line1.length > 0').isRequired('Zip is required').isNumber('Zip must be a number')
                .customValidator('Invalid Zip Code, does not match with City and State', cityAndStateAsyncValidatorFn),
        ];

        return personValidations;
    }

}



// component
import { PersonValidationPolicy } from './simple.person.validation.policy';
import { NgxValidationRunnerService } from 'ngx-model-validator';

public PERSON_POLICY_NAME = 'simple-person-validation';
public PERSON_VALIDATION_FN = PersonValidationPolicy;

constructor(private valRunnerSvc: NgxValidationRunnerService){ }

validate() {
    this.valRunnerSvc.validate(this.PERSON_POLICY_NAME, this.person)
}
  • Simple validation without error display on field, errors to be handled in component
<ngx-component-validator [model]="person" [policy]="PERSON_POLICY_NAME" [policyFnRef]="PERSON_VALIDATION_FN"></ngx-component-validator>

<input matInput placeholder="First Name" [(ngModel)]="person.firstName" />
// policy
validatorHelper.validateFor('firstName').isRequired('First Name is required'),

// component
this.valRunnerSvc.validate(this.PERSON_POLICY_NAME, this.person).subscribe(result=>{
      if(result && result.length > 0) {
         console.error('Validation error found');
      }
    });
  • Simple validation with required * and error display on field
<ngx-component-validator [model]="person" [policy]="PERSON_POLICY_NAME" [policyFnRef]="PERSON_VALIDATION_FN"></ngx-component-validator>

<input matInput placeholder="First Name" ngxMatValidate ngxMatRequired [model]="person" validateProperty="firstName" [policy]="PERSON_POLICY_NAME" [(ngModel)]="person.firstName" />
// policy
validatorHelper.validateFor('firstName').isRequired('First Name is required'),

// component
this.valRunnerSvc.validate(this.PERSON_POLICY_NAME, this.person)

Development server

Run npm run start for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Running unit tests

Run ng test to execute the unit tests via Karma.