
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
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 3 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
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.