
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
young-validator
Advanced tools
a simple, functional schema validator for javscript.
import { any, array, boolean, dictionary, enumeration, number, object, or, string } from 'young-validator';
// string
const stringValidator = string({ minLength: 10 });
stringValidator('test string'); // OK
stringValidator('test'); // ERROR: value length must be at least 10
stringValidator(undefined); // ERROR: value must be defined
// number
const numberValidator = number({ optional: true, max: 1000, min: 0, strict: false });
numberValidator(23); // OK
numberValidator(undefined); // OK, because optional: true
numberValidator('44'); // OK, because strict: false
numberValidator(-10); // ERROR: value must be greater than 0
// boolean
const booleanValidator = boolean(); // config is always optional
booleanValidator(false); // OK
booleanValidator(''); // ERROR: value must be a boolean
// array
const arrayConfig = array({ maxLength: 5 });
// any validator can be passed into the second argument
const anyArrayValidator = arrayConfig(any());
const stringArrayValidator = arrayConfig(string());
const numberArrayValidator = arrayConfig(number());
const arrayArrayValidator = arrayConfig(anyArrayValidator);
anyArrayValidator(['string', 100, {}]); // OK
stringArrayValidator(['string', 100, {}]); // ERROR: element at index 1 must be a string
numberArrayValidator(['string', 100, {}]); // ERROR: element at index 0 must be a number
arrayArrayValidator(['string', 100, {}]); // ERROR: element at index 0 must be an array
anyArrayValidator([1, 2, 3, 4, 5, 6]); // ERROR: array length must be less than or equal to 5
arrayArrayValidator([[1, 2, 3], [1, 2, 3]]); // OK
// dictionary
const dictionaryConfig = dictionary();
// same as array, any validator can be passed into the second argument
const stringDictionaryValidator = dictionaryConfig(string());
const arrayDictionaryValidator = dictionaryConfig(array()(any()));
const anyDictionaryValidator = dictionaryConfig(any());
anyDictionaryValidator({}); // OK
stringDictionaryValidator({}); // OK
stringDictionaryValidator({ a: 'a' }); // OK
stringDictionaryValidator({ a: 1 }); // ERROR: value at property a must be a string
arrayDictionaryValidator({ a: [], b: {} }); // ERROR: value at property b must be an array
arrayDictionaryValidator({ a: [], b: [] }); // OK
// enum
// takes a typescript enum or equivelant object
enum Example {
One = 'one',
Two = 'two',
}
enum AnotherEnum {
Three = 'three',
Four = 'four',
}
const enumValidator = enumeration()(Example);
enumValidator(Example.One); // OK
enumValidator(AnotherEnum.Four); // ERROR: 'four' does not exist in ['one','two']
// object
// a dictionary of any validators can be passed into the second argument
const objectAValidator = object()({
propA: string({ optional: true }),
propB: number(),
});
const objectBValidator = object()({
propC: objectAValidator,
propD: any(),
});
objectAValidator({ propB: 123 }); // OK
objectAValidator({ propA: 'string', propB: 123 }); // OK
objectAValidator({ propA: 'string', propB: 123, propC: true }); // OK, use option trim:true to trim extra props. passes test though
objectAValidator({ propA: 'string' }); // ERROR: { propB: must be defined }
objectAValidator({ propA: false }); // ERROR: { propB: must be defined, propA: must be a string }
objectBValidator({ propC: { propB: 123 }, propD: true }); // OK
objectBValidator({ propC: {}, propD: true }); // ERROR: { propC: { propB: must be defined } }
// or
const stringOrNumberValidator = or(string(), number());
stringOrNumberValidator(123); // OK
stringOrNumberValidator('test'); // OK
stringOrNumberValidator(true); // ERROR
// ADVANCED
// these validators can be chained in any way you can think of to accomplish advanced scenarios
// inheritance
enum Gender {
Male,
Female,
Other,
}
const personSchema = {
name: string(),
age: number(),
gender: enumeration()(Gender),
};
const employeeValidator = object()({
...personSchema,
ssn: string(),
});
const parentValidator = object()({
...personSchema,
children: array()(object()(personSchema)),
});
// combination
const complexValidator = object()({
propA: array({ optional: true })(
object()({
propA_1: string(),
propA_2: string(),
propA_3: string(),
propA_4: object()({
propA_4_1: array()(
object()({
anotherProp: any(),
})
),
}),
})
),
propB: dictionary()(or(string(), number(), boolean())),
email: string({ match: /emailregexhere/ }),
});
This is a really new project that i built out of necessity. I plan to expand to more types, add more options for existing types, and improve performance in the coming months.
The codebase is very simple, has no dependecies, and should be fairly easy to adapt to your specific needs.
also, in the near future i will add support for custom validators.
FAQs
a simple, functional schema validator for javscript.
The npm package young-validator receives a total of 1 weekly downloads. As such, young-validator popularity was classified as not popular.
We found that young-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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

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.