
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.
ts-input-validator
Advanced tools
This is a library for making input validation, like the Laravel input validator does. Aiming to replicate the Laravel version but written in Typescript.
pnpm i ts-input-validator
import { Validator } from "ts-input-validator"
import type { validateType, finalValidationResult } from "ts-input-validator"
// data property is the input to validate
// key is the key to map the result with
// rules is the set of rules to apply, see Available Rules.
// you can directly apply the message when fails, by giving each rule with message.
// or you can override every default message, see Validation Setup.
const toValidate: validateType[] = [
{ data: 'test@email.com', key: 'email', rules: ['required', 'email','min:8'], message: { 'required': 'value must not be empty' } },
{ data: 'p@$$w0Rd', key: 'password', rules: 'required|password|min:8|max:20' },
{ data: 28, key: 'age', rules: 'required|number' },
{ data: 'fishing', key: 'hobby', rules: 'required|string', },
{ data: '12-12-2022', key: 'birth_date', rules: 'required|date', message: { date: 'value must be a valid birth date' } },
{ data: '5fcb09a4-f5cc-46ef-aa76-8d185e6490ab', key: 'uuid', rules: ['required', 'uuid'] },
{ data: 123.45, key: 'hutang', rules: ['required', 'decimal:2'] },
{ data: 200_000_000, key: 'gaji', rules: ['required', 'integer'] },
]
// failed or the first item is boolean value, indicating the result fails (if value is true) or success (if value is false).
// result or the second item is object that cotains the validation message if validation is not success, keyed by given validation key
const [ failed, result ]: finalValidationResult = Validator.validate(toValidate)
if(failed) {
console.log('failed')
return result // show the validation error message to the user
}
// validation is succeed, no input fails the validation. Continue the process
required
min
min:8
max
max:8
email
number
password
string
accepted
declined
boolean
date
new Date()
functionuuid
decimal
decimal:2
integer
after
new Date()
that the value is greater than param, usage: after:12-12-2024
before
new Date()
that the value is smaller than param, usage: before:12-12-2024
afterOrEqual
new Date()
that the value is greater or equal than param, usage: afterOrEqual:12-12-2024
beforeOrEqual
new Date()
that the value is smaller or equal than param, usage: beforeOrEqual:12-12-2024
minDigit
minDigit:10
maxDigit
maxDigit:225
alpha
alphaNumeric
url
array
object
equalTo
equalTo:stringExample
notEqualTo
notEqualTo:stringExample
in
notIn:'["element1", "element2", "element3"]'
or notIn:aReallyLongStringExample
notIn
notIn:'["element1", "element2", "element3"]'
or notIn:aReallyLongStringExample
regex
regex:/^(\w+):(\d+(\.\d+)?)$/
dateBetween
new Date()
) between 2 date, separated by comma (,). usage: dateBetween:01-01-2024,02-02-2024
numberBetween
numberBetween:10,100
You can override the validation settings, like override all default validation message, returning back the data that validated. see below type:
type validatorSetup = {
dataInResult?: boolean
returnInvalidOnly?: boolean
setMessage?: ValidationMessage
}
usage of this:
import type { ValidationMessage } from "ts-input-validator"
// make your custom validation message
// below contains all of the default validation message, you can customize it like you want.
// NOTE:
// - :attr will be replaced to the given validation key
// - validation value like :min will be replaced by the value
const customValidationMessage: ValidationMessage = {
string: ':attr must be a valid string',
number: ':attr must be a valid number',
boolean: ':attr must be a valid boolean',
required: ':attr field is required',
min: ':attr must be greater than :min',
max: ':attr must be less than :max',
email: ':attr must be a valid email',
password: ':attr must be a valid password',
accepted: ':attr must be accepted',
declined: ':attr must be rejected',
date: ':attr must be a valid date',
uuid: ':attr must be a valid uuid version 4',
decimal: ':attr must be a valid decimal with :decimal digit',
integer: ':attr must be a valid integer, larger than 0, no decimal, and not infinite',
after: ':attr must be greater than :after',
afterOrEqual: ':attr must be greater or equal to :after',
before: ':attr must be less than :before',
beforeOrEqual: ':attr must be less than or equal to :before',
minDigit: ':attr must have a minimum of :min_digit digits',
maxDigit: ':attr must have a maximum of :max_digit digits',
alpha: ':attr must contain only alphabetic characters',
alphaNumeric: ':attr must contain only alphanumeric characters',
url: ':attr must be a valid URL',
array: ':attr must be an array',
object: ':attr must be an object',
equalTo: ':attr must be equal to :equalTo',
notEqualTo: ':attr must not be equal to :notEqualTo',
in: ':attr must be one of the following values: :in',
notIn: ':attr must not be one of the following values: :notIn',
regex: ':attr must match the pattern :regex',
dateBetween: ':attr must match the date between :dateBetween',
numberBetween: ':attr must match the number between :numberBetween'
}
const [ failed, result ]: finalValidationResult = Validator.setup({
dataInResult: false, // true if you want to get the validated data, default false
returnInvalidOnly: true, // return the result that fails only, default true
setMessage: customValidationMessage // override all the default validation message
}).validate(toValidate)
FAQs
Typescript Input Validator
The npm package ts-input-validator receives a total of 20 weekly downloads. As such, ts-input-validator popularity was classified as not popular.
We found that ts-input-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.