
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
@ennis/valid
Advanced tools
Simple object validation. Leverages the javascript global type constructors for easier composition. Expects JSON-like values (doesn't mess with functions, dates, etc).
npm i @ennis/valid
const Val = require('@ennis/valid');
let schema = {
shouldBeArray: Array,
hopeItsANumber: Number,
nestedObject: {
inside: String,
},
arraysToo: [
{eachOne: String}
],
stringOrNum: Val.or(String, Number),
intsAreAnnoying: Number.isInteger,
upToYou: Val.optional(String)
}
Val.validate(schema, {
shouldBeArray: ['hi'],
hopeItsANumber: 13.5,
nestedObject: {
inside: 'good so far'
},
arraysToo: [
{eachOne: 'foo'},
{eachOne: 'bar'}
],
stringOrNum: 'string it is'
intsAreAnnoying: 12
});
// returns: {valid: true, errors: []}
Val.validate({
name: String,
age: val => val > 20
}, {
name: null,
age: 14
});
/* returns: {
valid: false,
errors: [
".name: expected string, got null",
".age: integer did not pass validator function"
]
}*/
All valid types and definitions are shown here:
| type | valid definitions |
|---|---|
| object | 'object', Object |
| array | 'array', Array |
| string | 'string', String |
| boolean | 'boolean', Boolean |
| number | 'number', Number |
| integer | 'integer', Number.isInteger |
| null | 'null', null |
| exists (not null/undefined) | 'exists' |
| validator function | value => true/false |
Recursively travels through an object or array to ensure all values are present and of the proper type. Accepts type definitions or validator functions. Schemas can include a single definition as the first item of an array. See usage example
Returns an object:
{
valid: true || false,
errors: ['human readable invalid features']
}
Atomic function of .validate(). Just checks the value against the type definition. Returns false or a string explaining the fault.
For example:
const {isInvalid } = require('@ennis/valid');
isInvalid(String, 13);
// returns 'expected string, got: integer'
isInvalid(Number, 13);
// returns false
Modify a type definition to make it optional. Present values of another type will be invalid.
For example:
const {optional, isInvalid } = require('@ennis/valid');
isInvalid(optional(String), null)
// false;
isInvalid(optional(String), 13)
// 'expected optional string, got: integer'
By default an empty array, even if it has a defined nested structure in the schema, is valid when its empty. This ensures there is atleast one item in the array. Has nonEmpty() alias.
For example:
const {validate, notEmpty} = Val;
let schema = {
users: notEmpty([
{id: Number, name: String}
])
}
validate(schema, {users: []})
// {valid: false, errors: ['.users: expected non empty array, got: array']}
validate(schema, {users: [
{id: 1, name: 'John'},
{id: 2, name: 'Jane'}
]})
// {valid: true, errors: []}
Accepts a list of type definitions (or a single aray). A present value of any of the types will be valid. Nested objects/arrays won't work.
For example:
const {or, isInvalid } = require('@ennis/valid');
isInvalid(or(String, Object), 15)
// 'expect string or object, got: integer';
isInvalid(or(String, Number), 13)
// false
FAQs
Simple object validation
We found that @ennis/valid 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
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.