Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

model-state-validation

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

model-state-validation

```bush npm install model-state-validation ```

  • 0.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Installation

npm install model-state-validation

How to use

Define your validator

import { IValidator, ModelState } from "model-state-validation";
import { LoginModel } from "./LoginModel";

export class LoginValidator implements IValidator<LoginModel> {
    public validate(model: LoginModel): ModelState {
        const modelState = new ModelState();

        if (!this.usernameIsValid(model.username)) {
            modelState.addError(
                nameof<LoginModel>((o) => o.username),
                "Login is invalid."
            );
        }

        if (!this.passwordIsValid(model.password)) {
            modelState.addError(
                nameof<LoginModel>((o) => o.password),
                "Password is invalid."
            );
        }

        return modelState;
    }

    public usernameIsValid(username: any): boolean {
        return typeof username === "string" && username.length > 0;
    }

    public passwordIsValid(password: any): boolean {
        const regexp = new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/);
        return password && regexp.test(password);
    }
}

And validate your model somewhere

import { ModelState } from "model-state-validation";
import { LoginModel } from "./LoginModel";
import { LoginModelValidator } from "./LoginModelValidator";

export class Somewhere {
    public doSomething(myLoginModel: LoginModel) {
        const validator = new LoginModelValidator();
        const modelState: ModelState = validator.validate(myLoginModel);

        if (modelState.isInvalid()) {
            console.log(modelState.getErrors());
            return;
        }
    }
}

FAQs

Package last updated on 29 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc