
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@jaris/validator
Advanced tools
@jaris/validator
Easily validate any data using an easily testable and extendable approach. AKA plain functions.
import validate from '@jaris/validator';
import { optional, required, string, object } from '@jaris/validator/rules';
const userController = {
async store(ctx) {
const { data, errors } = await validate(ctx.request.body, {
firstName: [required(), string()],
lastName: [optional(), string()],
team: [
optional(),
object({
name: [string()],
sport: [string()],
}),
],
});
if (errors) {
return errorResponse(errors);
}
const user = createSomeUser(data);
return successResponse(user);
},
};
export default userController;
When you want to check that all items in an array meet a certain condition.
await validate(
{ evenNumbers: [2, 4, 6] },
{
evenNumbers: [array(), all(item => item % 2 === 0)],
},
);
Makes sure that the item being validated is in the provided array
await validate(
{ day: 'Saturday' },
{
day: [string(), allowed(['Saturday', 'Sunday'])],
},
);
Makes sure that the value being validated is an array
await validate(
{ days: ['Monday', 'Wednesday'] },
{
day: [array()],
},
);
Makes sure that the value being validated is either true or false
await validate(
{ termsOfService: true },
{
termsOfService: [boolean()],
},
);
Alias of allowed
Checks that the value being validated is a hex color
await validate(
{ color: '#AFAFAF' },
{
color: [hexColor()],
},
);
Takes a function that gets the value being validated passed as the first argument and either returns true or false.
await validate(
{ username: 'nehero' },
{
username: [lambda(username => usernameDoesntExist(username))],
},
);
Verifies that the value being validated is of a certain length.
await validate(
{ username: 'nehero' },
{
username: [length(0, 64)],
},
);
Verifies that the value being validated is not greater than the provided value
await validate(
{ username: 'nehero' },
{
username: [max(64)],
},
);
Verifies that the value being validated is not less than the provided value
await validate(
{ username: 'nehero' },
{
username: [min(0)],
},
);
Verifies that the value being validated is not in the provided array
await validate(
{ day: 'Tuesday' },
{
day: [notAllowed(['Saturday', 'Sunday'])],
},
);
alias of notAllowed
Verifies that the value being validated is of type number
await validate(
{ day: 16 },
{
day: [number()],
},
);
Verifies that the value being validated is of type object
and allows you to test any keys.
await validate(
{ team: { name: 'Test Team', userIds: [4, 5, 6] } },
{
team: [
required(),
object({
name: [required(), string()],
userIds: [required(), array(), all(userId => userIdExists(id))],
}),
],
},
);
Allows the value to not be present in the body. Note that null
and other falsy values still count as being present. Does not continue on with other validators in the same array if value isn't present.
await validate(
{ firstName: 'Ozzie' },
{
firstName: [required(), string()],
lastName: [optional(), string()],
},
);
Allows the value to be tested against a regex pattern.
await validate(
{ username: 'nehero' },
{
username: [regex(/[A-Za-z0-9]+/g)],
},
);
Ensures that the value is present in the body
await validate(
{ firstName: 'Ozzie' },
{
firstName: [required(), string()],
lastName: [optional(), string()],
},
);
Only marks the value as required if the other key specified is present on the body. Works with nested values using dot
syntax.
await validate(
{ firstName: 'Ozzie' },
{
firstName: [required(), string()],
lastName: [requiredWith('team.name'), string()],
team: [
optional(),
object({
name: [string()],
}),
],
},
);
Ensures the value is a string.
await validate(
{ firstName: 'Ozzie' },
{
firstName: [required(), string()],
},
);
Ensures the value is of the provided type. Evaluated using typeof
. For array either use array()
helper or pass in Array
.
await validate(
{ firstName: 'Ozzie' },
{
firstName: [required(), type('string')],
},
);
FAQs
> TODO: description
We found that @jaris/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
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.