@sumor/validator
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "@sumor/validator", | ||
"description": "This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"license": "MIT", | ||
@@ -16,3 +16,4 @@ "repository": "sumor-cloud/validator", | ||
"dependencies": { | ||
"@sumor/i18n": "^1.0.1" | ||
"@sumor/i18n": "^1.0.2", | ||
"@sumor/error": "^1.0.3" | ||
}, | ||
@@ -19,0 +20,0 @@ "devDependencies": { |
@@ -51,3 +51,3 @@ # validator | ||
{ | ||
id: 'ONLY_CHAR_DIGIT', | ||
code: 'ONLY_CHAR_DIGIT', | ||
expression: '^[a-zA-Z0-9]*$', | ||
@@ -58,3 +58,3 @@ message: 'only allow a-z, A-Z, 0-9' | ||
{ | ||
id: 'INCLUDE_DEMO', | ||
code: 'INCLUDE_DEMO', | ||
expression: 'demo', | ||
@@ -65,3 +65,3 @@ message: 'need include demo' | ||
{ | ||
id: 'LENGTH_GREATER_THAN_5', | ||
code: 'LENGTH_GREATER_THAN_5', | ||
expression: value => { | ||
@@ -122,3 +122,3 @@ return value.length > 5 | ||
{ | ||
id: 'GREATER_THAN_5', | ||
code: 'GREATER_THAN_5', | ||
expression: value => { | ||
@@ -224,1 +224,35 @@ return value > 5 | ||
``` | ||
### 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](https://www.npmjs.com/package/@sumor/error) | ||
```js | ||
import { validate } from '@sumor/validator' | ||
const parameterInfo = { | ||
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) | ||
/* | ||
SumorError | ||
{ | ||
code: 'LENGTH_GREATER_THAN_5', | ||
message: 'length should be greater than 5' | ||
} | ||
*/ | ||
``` |
@@ -1,14 +0,5 @@ | ||
import getI18n from './getI18n.js' | ||
import getI18n from '@sumor/i18n' | ||
import getI18nConfig from './getI18nConfig.js' | ||
export default (language, info) => { | ||
info = info || {} | ||
const i18n = info.i18n || {} | ||
const config = { | ||
origin: {}, | ||
...i18n | ||
} | ||
for (const i in info.rule) { | ||
const rule = info.rule[i] | ||
config.origin[rule.id] = rule.message | ||
} | ||
return getI18n(language, config) | ||
return getI18n(language, getI18nConfig(language, info)) | ||
} |
export default { | ||
origin: { | ||
SUMOR_REQUIRED: 'Mandatory field', | ||
SUMOR_STRING_LENGTH: 'Length must be less than or equal to {length} characters', | ||
SUMOR_INVALID_NUMBER: 'Invalid number {value}', | ||
SUMOR_NUMBER_LENGTH: 'Length must be less than or equal to {length} digits' | ||
}, | ||
en: { | ||
@@ -3,0 +9,0 @@ SUMOR_REQUIRED: 'Mandatory field', |
@@ -7,5 +7,7 @@ import parseInfo from './parseInfo.js' | ||
import getI18n from './i18n/index.js' | ||
import getError from './error/index.js' | ||
const format = (info, value) => { | ||
info = parseInfo(info) | ||
info.error = !!info.error // if error is true, will return error object | ||
@@ -54,3 +56,3 @@ let formattedValue = value | ||
if (!result) { | ||
messages.push(rule.id) | ||
messages.push(rule.code) | ||
} | ||
@@ -60,7 +62,6 @@ } | ||
const i18n = getI18n(language, info) | ||
messages = messages.map(message => { | ||
return { | ||
id: message, | ||
message: i18n(message, { | ||
if (info.error) { | ||
const ValidationError = getError(language, info) | ||
messages = messages.map(message => { | ||
return new ValidationError(message, { | ||
value, | ||
@@ -70,4 +71,16 @@ currentLength: valueLength, | ||
}) | ||
} | ||
}) | ||
}) | ||
} else { | ||
const i18n = getI18n(language, info) | ||
messages = messages.map(message => { | ||
return { | ||
code: message, | ||
message: i18n(message, { | ||
value, | ||
currentLength: valueLength, | ||
...info | ||
}) | ||
} | ||
}) | ||
} | ||
@@ -74,0 +87,0 @@ return messages |
@@ -22,3 +22,3 @@ export default (info, value) => { | ||
// if(!regexp.test(value)){ | ||
// messages.push(rule.id) | ||
// messages.push(rule.code) | ||
// } | ||
@@ -25,0 +25,0 @@ // } |
@@ -25,3 +25,3 @@ export default info => { | ||
const rule = info.rule[i] | ||
rule.id = rule.id || `TMP_RULE_${i + 1}` | ||
rule.code = rule.code || `TMP_RULE_${i + 1}` | ||
rule.message = rule.message || '' | ||
@@ -28,0 +28,0 @@ } |
@@ -16,3 +16,3 @@ export default (info, value) => { | ||
if (!regexp.test(value)) { | ||
messages.push(rule.id) | ||
messages.push(rule.code) | ||
} | ||
@@ -19,0 +19,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18341
14
306
253
2
+ Added@sumor/error@^1.0.3
+ Added@sumor/error@1.0.8(transitive)
Updated@sumor/i18n@^1.0.2