validator
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.
Installation
npm i @sumor/validator --save
Prerequisites
Node.JS version
Require Node.JS version 16.x or above
require Node.JS ES module
As this package is written in ES module,
please change the following code in your package.json
file:
{
"type": "module"
}
Usage
Validate String Usage
import { validate } from '@sumor/validator'
const parameterInfo = {
type: 'string',
required: true,
length: 10,
rule: [
{
code: 'ONLY_CHAR_DIGIT',
expression: '^[a-zA-Z0-9]*$',
message: 'only allow a-z, A-Z, 0-9'
},
{
code: 'INCLUDE_DEMO',
expression: 'demo',
message: 'need include demo'
},
{
code: '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)
const messages3 = validate(parameterInfo, 'demo!')
console.log(messages3)
const messages4 = validate(parameterInfo, 'de!mo')
console.log(messages4)
const messages5 = validate(parameterInfo, 'de')
console.log(messages5)
const messages6 = validate(parameterInfo, 'de', 'zh')
console.log(messages6)
const messages7 = validate(parameterInfo, 'de', 'zh-TW')
console.log(messages7)
Validate Number Usage
import { validate } from '@sumor/validator'
const parameterInfo = {
type: 'number',
required: true,
rule: [
{
code: '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)
const messages3 = validate(parameterInfo, 4)
console.log(messages3)
const messages4 = validate(parameterInfo, 4, 'zh')
console.log(messages4)
const messages5 = validate(parameterInfo, 4, 'zh-TW')
console.log(messages5)
Format String Usage
Trim Usage
will remove the useless space for prefix and suffix
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'string',
trim: true
}
const value = format(parameterInfo, ' demo ')
console.log(value)
Lowercase Usage
will convert the string to lowercase
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'string',
lowercase: true
}
const value = format(parameterInfo, 'Demo')
console.log(value)
Uppercase Usage
will convert the string to uppercase
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'string',
uppercase: true
}
const value = format(parameterInfo, 'Demo')
console.log(value)
Format Number Usage
import { format } from '@sumor/validator'
const parameterInfo = {
type: 'number',
decimal: 2
}
const value1 = format(parameterInfo, 1.234)
console.log(value1)
const value2 = format(parameterInfo, '1.234')
console.log(value2)
Enable Output Error
If you pass error:true, response will be an SumorError object.
You can change language and export json support by @sumor/error
import { validate } from '@sumor/validator'
const parameterInfo = {
error: true,
type: 'string',
required: true,
length: 10,
rule: [
{
code: 'LENGTH_GREATER_THAN_5',
expression: value => {
return value.length > 5
},
message: 'length should be greater than 5'
}
]
}
const messages = validate(parameterInfo, 'demo123456', 'en', true)
console.log(messages)