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 - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

2

dist/Errors.js

@@ -9,3 +9,3 @@ export class Errors {

// @ts-ignore
return typeof this[fieldName] !== undefined;
return typeof this[fieldName] !== "undefined";
}

@@ -12,0 +12,0 @@ addField(fieldName, value) {

{
"name": "model-state-validation",
"version": "0.8.0",
"version": "0.9.0",
"description": "",

@@ -5,0 +5,0 @@ "contributors": [

@@ -1,2 +0,63 @@

# model-state-validation
# Installation
```bush
npm install model-state-validation
```
# How to use
Define your validator
```ts
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
```ts
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;
}
}
}
```

Sorry, the diff of this file is not supported yet

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