Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Parameter = require('parameter')
- const p = new Parameter()
- // custom checker to check from field
- p.addRule('from', function(rule, value) {
- if (typeof value === 'string') {
- return Parameter.TYPE_MAP.email.call(this, { allowEmpty: false }, value)
- } else if (typeof value === 'object') {
- const objectRule = {
- name: {
- type: 'string',
- required: true,
- },
- email: {
- type: 'email',
- required: true,
- },
- }
- return Parameter.TYPE_MAP.object.call(this, { rule: objectRule }, value)
- } else {
- return this.t('should provide a string or object')
- }
- })
- // custom checker to check to field
- p.addRule('to', function(rule, value) {
- const objectRule = {
- email: {
- type: 'email',
- required: true,
- },
- }
- if (typeof value === 'string') {
- return Parameter.TYPE_MAP.email.call(this, { allowEmpty: false }, value)
- } else if (Object.prototype.toString.call(value) === '[object Object]') {
- return Parameter.TYPE_MAP.object.call(this, { rule: objectRule }, value)
- } else if (Array.isArray(value)) {
- const aryRule = {
- itemType: 'to',
- }
- return Parameter.TYPE_MAP.array.call(this, aryRule, value)
- } else {
- return this.t('should provide a string or an array')
- }
- })
- // ps. parameter only support validate an object(can not be an array)
- const rule = {
- subject: 'string',
- from: 'from',
- to: 'to',
- data: 'object',
- id: 'string',
- }
Add Comment
Please, Sign In to add comment