Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@sumor/validator
Advanced tools
This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.
A Sumor Cloud Tool.
More Documentation
This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.
npm i @sumor/validator --save
Require Node.JS version 16.x or above
As this package is written in ES module,
please change the following code in your package.json
file:
{
"type": "module"
}
import { validate } from '@sumor/validator'
const parameterInfo = {
type: 'string',
required: true,
length: 10,
rule: [
// only allow a-z, A-Z, 0-9
{
id: 'ONLY_CHAR_DIGIT',
expression: '^[a-zA-Z0-9]*$',
message: 'only allow a-z, A-Z, 0-9'
},
// need include demo
{
id: 'INCLUDE_DEMO',
expression: 'demo',
message: 'need include demo'
},
// use function to check
{
id: 'LENGTH_GREATER_THAN_5',
expression: value => {
return value.length > 5
},
message: 'length should be greater than 5'
}
],
i18n: {
zh: {
ONLY_CHAR_DIGIT: '只允许输入字母和数字',
INCLUDE_DEMO: '需要包含demo',
LENGTH_GREATER_THAN_5: '长度应大于5'
},
'zh-TW': {
ONLY_CHAR_DIGIT: '只允許輸入字母和數字',
INCLUDE_DEMO: '需要包含demo',
LENGTH_GREATER_THAN_5: '長度應大於5'
}
}
}
const messages1 = validate(parameterInfo, 'demo123456')
console.log(messages1) // []
const messages2 = validate(parameterInfo, 'de1234567')
console.log(messages2) // [ 'only allow a-z, A-Z, 0-9' ]
const messages3 = validate(parameterInfo, 'demo!')
console.log(messages3) // [ 'only allow a-z, A-Z, 0-9', 'need include demo' ]
const messages4 = validate(parameterInfo, 'de!mo')
console.log(messages4) // [ 'only allow a-z, A-Z, 0-9', 'need include demo' ]
const messages5 = validate(parameterInfo, 'de')
console.log(messages5) // [ 'only allow a-z, A-Z, 0-9', 'need include demo', 'length should be greater than 5' ]
// translate to zh
const messages6 = validate(parameterInfo, 'de', 'zh')
console.log(messages6) // [ '只允许输入字母和数字', '需要包含demo', '长度应大于5' ]
// translate to zh-TW
const messages7 = validate(parameterInfo, 'de', 'zh-TW')
console.log(messages7) // [ '只允許輸入字母和數字', '需要包含demo', '長度應大於5' ]
import { validate } from '@sumor/validator'
const parameterInfo = {
type: 'number',
required: true,
rule: [
// need greater than 5
{
id: 'GREATER_THAN_5',
expression: value => {
return value > 5
},
message: 'value should be greater than 5'
}
],
i18n: {
zh: {
GREATER_THAN_5: '值应大于5'
},
'zh-TW': {
GREATER_THAN_5: '值應大於5'
}
}
}
const messages1 = validate(parameterInfo, 6)
console.log(messages1) // []
const messages2 = validate(parameterInfo, 5)
console.log(messages2) // [ 'value should be greater than 5' ]
const messages3 = validate(parameterInfo, 4)
console.log(messages3) // [ 'value should be greater than 5' ]
// translate to zh
const messages4 = validate(parameterInfo, 4, 'zh')
console.log(messages4) // [ '值应大于5' ]
// translate to zh-TW
const messages5 = validate(parameterInfo, 4, 'zh-TW')
console.log(messages5) // [ '值應大於5' ]
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'string'
}
const value1 = format(parameterInfo, ' demo ')
console.log(value1) // will print "demo", useless space will be removed
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'number',
decimal: 2
}
const value1 = format(parameterInfo, 1.234)
console.log(value1) // will print 1.23, only keep 2 decimal
const value2 = format(parameterInfo, '1.234')
console.log(value2) // will convert to number 1.23, only keep 2 decimal
FAQs
This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.
We found that @sumor/validator demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.