model-state-validation
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9143
64