Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
rokot-validate
Advanced tools
Rocketmakers Rokot Platform - Validation
Weekly downloads
Readme
Rokot - Rocketmakers TypeScript NodeJs Platform
A typescript framework for Validation.
This library extends the validate.js
npm with additional validators.
It provides abstract Validator<T>
and ValidatorWithContext<T, TContext>
class to create validator instances
The only difference between the 2 validators is that ValidatorWithContext provides a method to obtain context data that is needed within the creation of the ConstraintSpec
NOTE: When an object is validated it is returned 'clean' (e.g. attributes NOT specified on the validator will be removed)
Install via npm
npm i rokot-validate -S
import {ValidatorWithContext, Validator, ConstraintSpec} from "rokot-validate";
export interface IEntity {
id: string;
members?: number[];
subEntities: ISubEntity[];
}
export interface ISubEntity {
id: string
}
// Create the 'Validator Spec'
class SubEntityValidator extends Validator<ISubEntity> {
constraints(): ConstraintSpec<ISubEntity> {
return {
id: this.mandatoryString()
}
}
}
export class EntityValidator extends ValidatorWithContext<IEntity, number>{
getContext(entity, isRootValidator) {
return Promise.resolve(1);
}
constraints(context: number) :ConstraintSpec<IEntity> {
//console.log("context", context);
return {
id: this.mandatoryString(),
deleteMe:{
absence: true
},
subEntities: {
array: true,
presence: true,
validator: new SubEntityValidator()
},
members: {
array: {
numericality: this.integerStrict({greaterThan:0})
}
}
};
}
}
const validator = new EntityValidator();
validator.validate({ id: "1",members:[1,2], subEntities:[{id:"2"}], extraneous:true })
.then((entity: IEntity) => {
// you now have a clean and verified entity
// e.g. {id: "1",members:[1,2], subEntities:[{id:"2"}] }
})
.catch(error => {
// the error might be a real error (if your custom functions failed)
// or most likely it will be a ValidationError (implements IValidationErrors)
})
Ensure the value is a string
Example: name is a mandatory string
name: {
presence: true,
string: true
}
Ensure the value is a boolean
Example: active is a mandatory boolean
active: {
presence: true,
boolean: true
}
Ensure the value is an array
Example: tags is an array
tags: {
array: true
}
Example: counts is a numeric array
counts: {
array: {
numericality: true
}
}
Ensure the property explicitly doesn't exist on the object
notAllowed: {
absence: true
}
Ensure the value is not null
or undefined
Allows a custom function (or functions) to determine validity
Example: single function to check existence in database
roleId: {
must: {
func: this.mustValidator<string>(id => db.checkRoleId(id)),
message: "does not exist"
}
}
Example: multiple functions to check existence in database, and perform secondary check
roleId: {
must: [{
func: this.mustValidator<string>(id => db.checkRoleId(id)),
message: "does not exist"
},{
func: this.mustValidator<string>(id => db.someOtherCheck(id)),
message: "does not pass other check"
}]
}
Example: single function to check existence in database (called for each item in array)
roleIds: {
array: true,
must: {
func: this.mustValidator<string>(id => db.checkRoleId(id)),
message: "does not exist"
}
}
Allows you to specify a child Validator for the property
Example: validate friend objects with a secondary validator
friends:{
array:true,
validator: new FriendValidator()
}
Declarative validating of javascript objects
Install node_modules
via npm
npm i
Install typings
typings install
Build the project (using typescript compiler)
npm run build
Test the project (builds before testing)
npm test
FAQs
Rocketmakers Rokot Platform - Validation
The npm package rokot-validate receives a total of 0 weekly downloads. As such, rokot-validate popularity was classified as not popular.
We found that rokot-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
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.