
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
variables-type-validator
Advanced tools
this package help to verify the type and characterstics of any variable provided
If you're not sure that what kind of input you are getting and you want to validate it before doing further process this package help you with this. anything which is not as per requirement it returns error.
Server-side usage
Install the library with
npm install --save variables-type-validator
let validator = require('variables-type-validator');
const valid = validator.validate({
variable_name: {
value:"dummy string",
type:"String",
errorMessage:"custom error message" //for custom message
},
})
this will check that if given value is string or not it return the object like below :-
console.log(valid)
/*{
valid:true,
message:"all variables are valid"
}*/
type parameter accept four values
this module also help you with other characteristics of variable like
this parameter define is the value of given variable is required or not.
you can also set default value to any variable if its value is empty or null.
you can also define the length of the value of and variable. The length parameter accept an object. the length of the variable can be define as below
for example :-
const valid = validator.validate({
variable_name:{
value:[1,2,3,4,5,6,7],
type:"Array",
default:[1,2,3,4,5],
required:true,
length:{
gte:5,
lte:10
}
},
})
you can also pre define the schema for dataset you get it helps to validate the data and avoid unnecessary values.
first you have to register a schema an give it a name
const validator = require('validator');
validator.createSchema("my_schema",{
variable1:{
type:"Array",
required:true,
length:{
gte:5,
lte:10
}
},
variable2:{
type:"String",
required:false,
errorMessage:"custom error message" //for custom message
}
})
you can use same variable as you did in validate methods all parameters will work same as it does in validate method.
validating throw registered schema is super easy you just need to call the validateBySchema method.
const valid = validator.validateBySchema("my_schema",{
variable1:[1,2,3,4,5,6],
variable2:"sample text"
})
and this is it. now this method find the schema you entered and validate the data which you inserted.
you can also restrict the unwanted variable by defining any parameter in schema options.
validator.createSchema("my_schema",{
variable1:{
type:"String",
required:false
}
},{any:false})
now if you insert data which has other variable unlike you define it will throw an response like below
const valid = validator.validateBySchema("my_schema",{
variable1:"sample text",
variable2:10
})
console.log(valid);
/*
{
result: false,
message: 'variable variable2 is not allowed in schema'
}
*/
it simple reject all the other variable which are not set in a Schema.
Options accept following values
| Parameter | Optional | Default | Accept | Description |
|---|---|---|---|---|
| type | No | no default value | 'String', 'Number', 'Array', 'Object', 'Boolean' | defines type of value accepted for the required data. |
| required | Yes | false | true/false | define the value of given variable is required or not. |
| default | Yes | no default value | default value depend on type parameter | set default value to any variable if its value is empty or null. |
| length | Yes | no default value | accept object | object contains following values 'gte', 'lte', 'eq', 'gt', 'lt' check above example for details |
| restrictSpecialCharacters | Yes | false | true/false | restrict special characters in text (only work with 'String' type variables) |
=> if type of given variable does not matched
=> if required condition does not match
variable </your variable name> cannot be null
=> if length of given variable does not matched
this package is in its initial age so you may face some bugs and difficulties by using this but i am intresterd to know about your thoughts regarding this package so i can develop it more further and i hope that some of you may interested helping me with this package and give me new ideas that help me building this more further.
you can mail me your reviews and idea to my gmail account aliasgherbadshah128@gmail.com
FAQs
this package help to verify the type and characterstics of any variable provided
The npm package variables-type-validator receives a total of 1 weekly downloads. As such, variables-type-validator popularity was classified as not popular.
We found that variables-type-validator 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
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.