Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
model-validate
Advanced tools
MVC validation for JavaScript
Class for synchronous validation.
Parameters
validators
(optional, default {}
)Validate a model
according to the validators set up in mask
Parameters
model
Object Data to be validated Ex: { phone : "123-456-7890" }
mask
Object Validators to be applied to model. Ex: { phone : ['required', 'phone']}
The following validators are provided by default
import {SyncValidator} from 'model-validate';
// Get a validator with just the built-in validators
let validator = new SyncValidator();
// sample data including some errors
let model = {
address : {
firstName : 'John',
address1 : '123 SomeRoad',
address2 : 'apt 4',
city : 'New York',
state : ' ',
zip : '123456',
},
phone : '1-(123)-123-1234',
}
// get validation results
let results = validatator.validate(model, {
// the mask object mirrors the structure of the model
address : {
firstName : ['required'],
lastName : ['required'],
address1 : ['required'],
city : ['required'],
state : ['required'],
zip : ['required', 'zip-code'],
},
phone : ['required', 'phone'],
})
// This returns the following structure
{
"address": {
"firstName": {
"required": {
"_valid": true
},
"_valid": true
},
"lastName": {
"required": {
"_valid": false,
"reason": "\'lastName\' is null or undefined"
},
"_valid": false
},
"address1": {
"required": {
"_valid": true
},
"_valid": true
},
"city": {
"required": {
"_valid": true
},
"_valid": true
},
"state": {
"required": {
"value": false,
"reason": "String is empty"
},
"_valid": false
},
"zip": {
"required": {
"_valid": true
},
"zip-code": {
"_valid": false,
"reason": "Zip must be 5 digits or have the full extended syntax"
},
"_valid": false
},
"_valid": false
},
"phone": {
"required": {
"_valid": true
},
"phone": {
"_valid": true
},
"_valid": true
},
"_valid": false
}
There is a _valid
property at every level that describes the validity of everything at that level and lower. This way
you can decide to use the data or not simply based on results._valid
, but you can show feedback to the user based on
the valididy of a specific validation.
For example model.address.zip
passes the required
validator, but not the zip-code
validator because it has 6
digits instead of 5. So results.address.zip.required._valid == true
, but results.address.zip.zip-code._valid == false
FAQs
Data validation framework for MVC
The npm package model-validate receives a total of 0 weekly downloads. As such, model-validate popularity was classified as not popular.
We found that model-validate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.