diff --git a/packages/formEditor/components/Panels/Config/components/LogicComponent.vue b/packages/formEditor/components/Panels/Config/components/LogicComponent.vue index 22204de..5ed55d0 100644 --- a/packages/formEditor/components/Panels/Config/components/LogicComponent.vue +++ b/packages/formEditor/components/Panels/Config/components/LogicComponent.vue @@ -148,6 +148,7 @@ const handleAction = (type) => { break case 2: if (tabs.value.every(tab => !tab.rules.length)) { + ER.state.logic = {} closeDialog() } else { const data = getData() diff --git a/packages/hooks/use-logic/index.js b/packages/hooks/use-logic/index.js index fbb1d2a..b8ef5da 100644 --- a/packages/hooks/use-logic/index.js +++ b/packages/hooks/use-logic/index.js @@ -1,5 +1,6 @@ import { computed, watch } from 'vue' import _ from 'lodash-es' +import utils from '@ER/utils' const findValidityRule = (state) => { const result = {} for (const logicType in state.logic) { @@ -32,19 +33,16 @@ const getDataType = (fieldType) => { } return result } -const equal = (logicValue, value, filedType) => { +const equal = (logicValue, value, fieldType) => { // console.log(logicValue) // console.log(value) - if (filedType === 'region') { + if (fieldType === 'region') { return _.includes(logicValue, value) } if (_.isString(value) || _.isNumber(value)) { return _.isEqual(logicValue, value) } if (_.isArray(value)) { - // if (/^(select|checkbox)$/.test(filedType)) { - // return logicValue.length === value.length && _.isEqual(_.chain(logicValue).clone().flattenDeep().sort().value(), _.chain(value).clone().flattenDeep().sort().value()) - // } return _.isEqual(_.chain(logicValue).clone().flattenDeep().sort().value(), _.chain(value).clone().flattenDeep().sort().value()) } if (_.isBoolean(value)) { @@ -54,7 +52,7 @@ const equal = (logicValue, value, filedType) => { const notEqual = (...e) => { return !equal(...e) } -const contains = (logicValue, value, filedType) => { +const contains = (logicValue, value, fieldType) => { if (_.isString(value)) { return logicValue.some((v) => _.includes(value, v)) } @@ -65,32 +63,44 @@ const contains = (logicValue, value, filedType) => { const notContains = (...e) => { return !contains(...e) } -export const validator = (logic, value, filed) => { +const empty = (logicValue, value, fieldType) => { + if (fieldType === 'rate') { + return value === 0 || utils.isEmpty(value) + } + return utils.isEmpty(value) +} +const notEmpty = (...e) => { + return !empty(...e) +} +export const validator = (logic, value, field) => { let result = false - // console.log(filed) + // console.log(field) switch (logic.operator) { case 'equal': // result = logic.value === value - result = equal(logic.value, value, filed.type) + result = equal(logic.value, value, field.type) break case 'one_of': break case 'not_equal': - result = notEqual(logic.value, value, filed.type) + result = notEqual(logic.value, value, field.type) break case 'contains': - // console.log(logic.value) - // console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`) - // console.log(value) - // console.log(`field的值:${value} type: ${typeof value}`) - result = contains(logic.value, value, filed.type) + result = contains(logic.value, value, field.type) break case 'not_contain': - result = notContains(logic.value, value, filed.type) + result = notContains(logic.value, value, field.type) break case 'empty': + // console.log(logic.value) + // console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`) + // console.log(value) + // console.log(`field的值:${value} type: ${typeof value}`) + // console.log(field) + result = empty(logic.value, value, field.type) break case 'not_empty': + result = notEmpty(logic.value, value, field.type) break case 'greater_than': break @@ -118,6 +128,9 @@ const operatingShowHidden = (fields, rules) => { // console.log(operator(values.map((value, index) => validator(rule.if.conditions[index], value, getDataType(targetFields[index].type))))) console.log(operator(values.map((value, index) => validator(rule.if.conditions[index], value, targetFields[index])))) // console.log(values.map((value, index) => validator(rule.if.conditions[index], value, getDataType(targetFields[index].type)))) + }, { + immediate: true, + deep: true }) // console.log(rule.if.conditions.map(e => e.property)) }) diff --git a/packages/utils/generateFilterdata.js b/packages/utils/generateFilterdata.js index ceceb60..f7bf2d4 100644 --- a/packages/utils/generateFilterdata.js +++ b/packages/utils/generateFilterdata.js @@ -115,10 +115,22 @@ const generateIfFilterOptionsData = (activeTab, fields) => { } break case 'number': + filterNode.renderType = 'NUMBER' + filterNode.operatorKey = 'Number' + break case 'rate': + filterNode.renderType = 'NUMBER' + filterNode.operatorKey = 'Number' + break case 'slider': filterNode.renderType = 'NUMBER' filterNode.operatorKey = 'Number' + filterNode.excludeOperator = { + operator: [ + 'not_empty', + 'empty' + ] + } break case 'radio': filterNode.renderType = 'SELECT' @@ -152,7 +164,9 @@ const generateIfFilterOptionsData = (activeTab, fields) => { filterNode.excludeOperator = { operator: [ 'contains', - 'not_contain' + 'not_contain', + 'not_empty', + 'empty' ] } break diff --git a/packages/utils/utils.js b/packages/utils/utils.js new file mode 100644 index 0000000..c244fa1 --- /dev/null +++ b/packages/utils/utils.js @@ -0,0 +1 @@ +export const isEmpty = (v) => (v === '' || v === null || v === undefined || (Array.isArray(v) && !v.length)) diff --git a/test/logic/logic.test.js b/test/logic/logic.test.js index 84b6709..30d4e27 100644 --- a/test/logic/logic.test.js +++ b/test/logic/logic.test.js @@ -2,21 +2,21 @@ import { describe, assert, expect, test } from 'vitest' import { validator } from '@ER/hooks/use-logic' describe('validator', () => { test('Email', () => { - const filed = { type: 'input', label: 'Email', icon: 'email', key: 'input_dkY3cEvdfnJ-2Y6IP9pkf', id: 'dkY3cEvdfnJ-2Y6IP9pkf', options: { clearable: true, renderType: 2, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'input', label: 'Email', icon: 'email', key: 'input_dkY3cEvdfnJ-2Y6IP9pkf', id: 'dkY3cEvdfnJ-2Y6IP9pkf', options: { clearable: true, renderType: 2, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123' }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123' }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -25,7 +25,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -34,7 +34,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_contain', @@ -43,24 +43,60 @@ describe('validator', () => { ] }, '456', - filed)).toBeTruthy() + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Cellphone', () => { - const filed = { type: 'input', label: 'Cellphone', icon: 'cellphone', key: 'input_feokPGo0uqPDPGvcbbkrB', id: 'feokPGo0uqPDPGvcbbkrB', options: { clearable: true, renderType: 4, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'input', label: 'Cellphone', icon: 'cellphone', key: 'input_feokPGo0uqPDPGvcbbkrB', id: 'feokPGo0uqPDPGvcbbkrB', options: { clearable: true, renderType: 4, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123' }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123' }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -69,7 +105,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -78,24 +114,60 @@ describe('validator', () => { ] }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('URL', () => { - const filed = { type: 'input', label: 'URL', icon: 'url', key: 'input_8Des6TBcAhe5zmEl7x83q', id: '8Des6TBcAhe5zmEl7x83q', options: { clearable: true, renderType: 5, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'input', label: 'URL', icon: 'url', key: 'input_8Des6TBcAhe5zmEl7x83q', id: '8Des6TBcAhe5zmEl7x83q', options: { clearable: true, renderType: 5, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 'https://everright.site/' }, 'https://everright.site/', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: 'https://everright.site/' }, 'https://everright.site/', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -104,7 +176,7 @@ describe('validator', () => { ] }, 'https://everright.site/', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -113,10 +185,46 @@ describe('validator', () => { ] }, 'https://everright.site/', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) // test('Region', () => { - // const filed = { type: 'region', label: 'Region', icon: 'location', key: 'region_Yqz1snVkubaX2WG8xCEFs', id: 'Yqz1snVkubaX2WG8xCEFs', options: { placeholder: 'Please select', required: false, isShowLabel: true, labelWidth: 100, defaultValue: '', selectType: 3, filterable: true }, style: { width: { pc: '100%', mobile: '100%' } } } + // const field = { type: 'region', label: 'Region', icon: 'location', key: 'region_Yqz1snVkubaX2WG8xCEFs', id: 'Yqz1snVkubaX2WG8xCEFs', options: { placeholder: 'Please select', required: false, isShowLabel: true, labelWidth: 100, defaultValue: '', selectType: 3, filterable: true }, style: { width: { pc: '100%', mobile: '100%' } } } // expect( // validator({ // operator: 'equal', @@ -125,7 +233,7 @@ describe('validator', () => { // ] // }, // '110000', - // filed)).toBeTruthy() + // field)).toBeTruthy() // expect( // validator({ // operator: 'not_equal', @@ -134,24 +242,24 @@ describe('validator', () => { // ] // }, // '110000', - // filed)).toBeTruthy() + // field)).toBeTruthy() // }) test('ID number', () => { - const filed = { type: 'input', label: 'ID number', icon: 'ID', key: 'input_KElgoYLKSLp8rkKQA5CP-', id: 'KElgoYLKSLp8rkKQA5CP-', options: { clearable: true, renderType: 3, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'input', label: 'ID number', icon: 'ID', key: 'input_KElgoYLKSLp8rkKQA5CP-', id: 'KElgoYLKSLp8rkKQA5CP-', options: { clearable: true, renderType: 3, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123' }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123' }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -160,7 +268,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -169,24 +277,60 @@ describe('validator', () => { ] }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Input', () => { - const filed = { type: 'input', label: 'Input', icon: 'input', key: 'input_D7ijHbzIJUJ8THOhHqGXK', id: 'D7ijHbzIJUJ8THOhHqGXK', options: { clearable: true, isShowWordLimit: false, renderType: 1, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false, min: null, max: null }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'input', label: 'Input', icon: 'input', key: 'input_D7ijHbzIJUJ8THOhHqGXK', id: 'D7ijHbzIJUJ8THOhHqGXK', options: { clearable: true, isShowWordLimit: false, renderType: 1, disabled: false, showPassword: false, defaultValue: '', placeholder: 'Please enter', labelWidth: 100, isShowLabel: true, required: false, min: null, max: null }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123' }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123' }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -195,7 +339,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -204,24 +348,60 @@ describe('validator', () => { ] }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Textarea', () => { - const filed = { type: 'textarea', label: 'Textarea', icon: 'textarea', key: 'textarea_ZPRzSVZkMHOMYgWZBmrvR', id: 'ZPRzSVZkMHOMYgWZBmrvR', options: { clearable: true, isShowWordLimit: false, rows: 6, defaultValue: '', placeholder: 'Please enter', disabled: false, labelWidth: 100, isShowLabel: true, required: false, min: null, max: null }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'textarea', label: 'Textarea', icon: 'textarea', key: 'textarea_ZPRzSVZkMHOMYgWZBmrvR', id: 'ZPRzSVZkMHOMYgWZBmrvR', options: { clearable: true, isShowWordLimit: false, rows: 6, defaultValue: '', placeholder: 'Please enter', disabled: false, labelWidth: 100, isShowLabel: true, required: false, min: null, max: null }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123' }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123' }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -230,7 +410,7 @@ describe('validator', () => { ] }, '123', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -239,58 +419,166 @@ describe('validator', () => { ] }, '123', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Number', () => { - const filed = { type: 'number', label: 'Number', icon: 'numbers', key: 'number_3bdLUMFSVB7_YujfBJhBB', id: '3bdLUMFSVB7_YujfBJhBB', options: { min: 0, max: null, step: 1, precision: 0, disabled: false, controls: true, controlsPosition: true, defaultValue: null, labelWidth: 100, isShowLabel: true, required: false, minlength: null, maxlength: null }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'number', label: 'Number', icon: 'numbers', key: 'number_3bdLUMFSVB7_YujfBJhBB', id: '3bdLUMFSVB7_YujfBJhBB', options: { min: 0, max: null, step: 1, precision: 0, disabled: false, controls: true, controlsPosition: true, defaultValue: null, labelWidth: 100, isShowLabel: true, required: false, minlength: null, maxlength: null }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 123 }, 123.00, - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: 123 }, 123.00, - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Radio', () => { - const filed = { type: 'radio', label: 'Radio', icon: 'radio', key: 'radio_FQWZ5Tf_KQryjLOj_-Oso', id: 'FQWZ5Tf_KQryjLOj_-Oso', options: { dataKey: 'FQWZ5Tf_KQryjLOj_-Oso', displayStyle: 'block', defaultValue: '', labelWidth: 100, isShowLabel: true, disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'radio', label: 'Radio', icon: 'radio', key: 'radio_FQWZ5Tf_KQryjLOj_-Oso', id: 'FQWZ5Tf_KQryjLOj_-Oso', options: { dataKey: 'FQWZ5Tf_KQryjLOj_-Oso', displayStyle: 'block', defaultValue: '', labelWidth: 100, isShowLabel: true, disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 'CrvWR4TVdNRxvxty5iAT2' }, 'CrvWR4TVdNRxvxty5iAT2', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: 'CrvWR4TVdNRxvxty5iAT2' }, 'CrvWR4TVdNRxvxty5iAT2', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', value: ['CrvWR4TVdNRxvxty5iAT2'] }, 'CrvWR4TVdNRxvxty5iAT2', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'toBeFalsy', value: ['CrvWR4TVdNRxvxty5iAT2'] }, 'CrvWR4TVdNRxvxty5iAT2', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Checkbox', () => { - const filed = { type: 'checkbox', label: 'Checkbox', icon: 'checkbox', key: 'checkbox_qE0oMb42msqQ1M4N35F6P', id: 'qE0oMb42msqQ1M4N35F6P', options: { dataKey: 'qE0oMb42msqQ1M4N35F6P', displayStyle: 'block', defaultValue: [], labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'checkbox', label: 'Checkbox', icon: 'checkbox', key: 'checkbox_qE0oMb42msqQ1M4N35F6P', id: 'qE0oMb42msqQ1M4N35F6P', options: { dataKey: 'qE0oMb42msqQ1M4N35F6P', displayStyle: 'block', defaultValue: [], labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', @@ -301,7 +589,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'equal', @@ -313,7 +601,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', @@ -324,7 +612,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', @@ -336,7 +624,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'contains', @@ -348,7 +636,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'contains', @@ -360,7 +648,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj1' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_contain', @@ -372,7 +660,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_contain', @@ -384,18 +672,30 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj1' ], - filed)).toBeTruthy() + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + [], + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + [], + field)).toBeFalsy() }) test('Select', () => { - const filed = { type: 'select', label: 'Select', icon: 'dropdown0', key: 'select_lLyhZ6ThDBd2_O5JKOjzN', id: 'lLyhZ6ThDBd2_O5JKOjzN', options: { dataKey: 'lLyhZ6ThDBd2_O5JKOjzN', filterable: true, multiple: false, defaultValue: [], placeholder: 'Please select', labelWidth: 100, isShowLabel: true, disabled: false, clearable: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'select', label: 'Select', icon: 'dropdown0', key: 'select_lLyhZ6ThDBd2_O5JKOjzN', id: 'lLyhZ6ThDBd2_O5JKOjzN', options: { dataKey: 'lLyhZ6ThDBd2_O5JKOjzN', filterable: true, multiple: false, defaultValue: [], placeholder: 'Please select', labelWidth: 100, isShowLabel: true, disabled: false, clearable: true, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '0bZN9TQ0bePdjOewythuj' }, '0bZN9TQ0bePdjOewythuj', - filed)).toBeTruthy() - filed.options.multiple = true + field)).toBeTruthy() + field.options.multiple = true expect( validator({ operator: 'equal', @@ -406,7 +706,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'equal', @@ -418,16 +718,16 @@ describe('validator', () => { '0bZN9TQ0bePdjOewythuj', 'OzUgt2_0be-2f97bPu0yJ' ], - filed)).toBeFalsy() - filed.options.multiple = false + field)).toBeFalsy() + field.options.multiple = false expect( validator({ operator: 'not_equal', value: '0bZN9TQ0bePdjOewythuj' }, '0bZN9TQ0bePdjOewythuj', - filed)).toBeFalsy() - filed.options.multiple = true + field)).toBeFalsy() + field.options.multiple = true expect( validator({ operator: 'not_equal', @@ -438,7 +738,7 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', @@ -450,8 +750,8 @@ describe('validator', () => { '0bZN9TQ0bePdjOewythuj', 'OzUgt2_0be-2f97bPu0yJ' ], - filed)).toBeTruthy() - filed.options.multiple = false + field)).toBeTruthy() + field.options.multiple = false expect( validator({ operator: 'contains', @@ -460,8 +760,8 @@ describe('validator', () => { ] }, '0bZN9TQ0bePdjOewythuj', - filed)).toBeTruthy() - filed.options.multiple = true + field)).toBeTruthy() + field.options.multiple = true expect( validator({ operator: 'contains', @@ -472,8 +772,8 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeTruthy() - filed.options.multiple = false + field)).toBeTruthy() + field.options.multiple = false expect( validator({ operator: 'not_contain', @@ -482,8 +782,8 @@ describe('validator', () => { ] }, '0bZN9TQ0bePdjOewythuj', - filed)).toBeFalsy() - filed.options.multiple = true + field)).toBeFalsy() + field.options.multiple = true expect( validator({ operator: 'not_contain', @@ -494,133 +794,269 @@ describe('validator', () => { [ '0bZN9TQ0bePdjOewythuj' ], - filed)).toBeFalsy() + field)).toBeFalsy() + field.options.multiple = false + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + field.options.multiple = true + expect( + validator({ + operator: 'empty' + }, + [], + field)).toBeTruthy() + field.options.multiple = false + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() + field.options.multiple = true + expect( + validator({ + operator: 'not_empty' + }, + [], + field)).toBeFalsy() }) test('Time', () => { - const filed = { type: 'time', label: 'Time', icon: 'time', key: 'time_JfeEFqHMQbVI424FbFEHw', id: 'JfeEFqHMQbVI424FbFEHw', options: { clearable: true, format: 'HH时mm分ss秒', defaultValue: null, placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'time', label: 'Time', icon: 'time', key: 'time_JfeEFqHMQbVI424FbFEHw', id: 'JfeEFqHMQbVI424FbFEHw', options: { clearable: true, format: 'HH时mm分ss秒', defaultValue: null, placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '16:12:46' }, '16:12:46', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '16:12:46' }, '16:12:46', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) // test('Date', () => { - // const filed = { type: 'time', label: 'Time', icon: 'time', key: 'time_JfeEFqHMQbVI424FbFEHw', id: 'JfeEFqHMQbVI424FbFEHw', options: { clearable: true, format: 'HH时mm分ss秒', defaultValue: null, placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + // const field = { type: 'time', label: 'Time', icon: 'time', key: 'time_JfeEFqHMQbVI424FbFEHw', id: 'JfeEFqHMQbVI424FbFEHw', options: { clearable: true, format: 'HH时mm分ss秒', defaultValue: null, placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } // expect( // validator({ // operator: 'equal', // value: '16:12:46' // }, // '16:12:46', - // filed)).toBeTruthy() + // field)).toBeTruthy() // }) test('Rate', () => { - const filed = { type: 'rate', label: 'Rate', icon: 'rating', key: 'rate_h3rw6be0P5V5ciSTbD6Yx', id: 'h3rw6be0P5V5ciSTbD6Yx', options: { max: 5, allowHalf: true, defaultValue: 0, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'rate', label: 'Rate', icon: 'rating', key: 'rate_h3rw6be0P5V5ciSTbD6Yx', id: 'h3rw6be0P5V5ciSTbD6Yx', options: { max: 5, allowHalf: true, defaultValue: 0, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 3.5 }, 3.5, - filed)).toBeTruthy() - filed.options.allowHalf = false + field)).toBeTruthy() + field.options.allowHalf = false expect( validator({ operator: 'equal', value: 3 }, 3, - filed)).toBeTruthy() - filed.options.allowHalf = true + field)).toBeTruthy() + field.options.allowHalf = true expect( validator({ operator: 'not_equal', value: 3.5 }, 3.5, - filed)).toBeFalsy() - filed.options.allowHalf = false + field)).toBeFalsy() + field.options.allowHalf = false expect( validator({ operator: 'not_equal', value: 3 }, 3, - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + 0, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + 0, + field)).toBeFalsy() }) test('Switch', () => { - const filed = { type: 'switch', label: 'Switch', icon: 'switch', key: 'switch_XpvOZGZjVMTVdHWZiuehy', id: 'XpvOZGZjVMTVdHWZiuehy', options: { defaultValue: true, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'switch', label: 'Switch', icon: 'switch', key: 'switch_XpvOZGZjVMTVdHWZiuehy', id: 'XpvOZGZjVMTVdHWZiuehy', options: { defaultValue: true, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 1 }, true, - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'equal', value: 0 }, false, - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: 1 }, true, - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', value: 0 }, false, - filed)).toBeFalsy() + field)).toBeFalsy() }) test('Slider', () => { - const filed = { type: 'slider', label: 'Slider', icon: 'slider', key: 'slider_DnNgjeX1vs6MTUd8IuMqO', id: 'DnNgjeX1vs6MTUd8IuMqO', options: { min: 0, max: 100, step: 1, defaultValue: 0, placeholder: '', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'slider', label: 'Slider', icon: 'slider', key: 'slider_DnNgjeX1vs6MTUd8IuMqO', id: 'DnNgjeX1vs6MTUd8IuMqO', options: { min: 0, max: 100, step: 1, defaultValue: 0, placeholder: '', labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: 4 }, 4, - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: 4 }, 4, - filed)).toBeFalsy() + field)).toBeFalsy() }) test('Html', () => { - const filed = { type: 'html', label: 'Html', icon: 'html', key: 'html_cBsbj6NW7wNicW2umHzsE', id: 'cBsbj6NW7wNicW2umHzsE', options: { defaultValue: '
123
', placeholder: 'Please enter', action: 'http://localhost:8001/Everright-api/lowCode/uploads', size: 1, labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'html', label: 'Html', icon: 'html', key: 'html_cBsbj6NW7wNicW2umHzsE', id: 'cBsbj6NW7wNicW2umHzsE', options: { defaultValue: '123
', placeholder: 'Please enter', action: 'http://localhost:8001/Everright-api/lowCode/uploads', size: 1, labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', value: '123
' }, '123
', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_equal', value: '123
' }, '123
', - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'contains', @@ -629,7 +1065,7 @@ describe('validator', () => { ] }, '123
', - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'not_contain', @@ -638,10 +1074,46 @@ describe('validator', () => { ] }, '123
', - filed)).toBeFalsy() + field)).toBeFalsy() + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) test('Cascader', () => { - const filed = { type: 'cascader', label: 'Cascader', icon: 'cascader', key: 'cascader_KFrOTULCQ0EAcMcIQTd_v', id: 'KFrOTULCQ0EAcMcIQTd_v', options: { filterable: true, multiple: false, checkStrictly: true, defaultValue: [], placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false, clearable: true, dataKey: 'KFrOTULCQ0EAcMcIQTd_v' }, style: { width: { pc: '100%', mobile: '100%' } } } + const field = { type: 'cascader', label: 'Cascader', icon: 'cascader', key: 'cascader_KFrOTULCQ0EAcMcIQTd_v', id: 'KFrOTULCQ0EAcMcIQTd_v', options: { filterable: true, multiple: false, checkStrictly: true, defaultValue: [], placeholder: 'Please select', labelWidth: 100, isShowLabel: true, required: false, disabled: false, clearable: true, dataKey: 'KFrOTULCQ0EAcMcIQTd_v' }, style: { width: { pc: '100%', mobile: '100%' } } } expect( validator({ operator: 'equal', @@ -654,7 +1126,7 @@ describe('validator', () => { 'MA47BXTJLl0Uf6dsw3hcI', '132RPqxAbuGTp-ZW-_Lav' ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'equal', @@ -666,8 +1138,8 @@ describe('validator', () => { [ 'MA47BXTJLl0Uf6dsw3hcI' ], - filed)).toBeFalsy() - filed.options.multiple = true + field)).toBeFalsy() + field.options.multiple = true expect( validator({ operator: 'equal', @@ -680,7 +1152,7 @@ describe('validator', () => { ['MA47BXTJLl0Uf6dsw3hcI', 'u-980ifeIW4HcE6JgUl5s'], ['MA47BXTJLl0Uf6dsw3hcI', '132RPqxAbuGTp-ZW-_Lav'] ], - filed)).toBeTruthy() + field)).toBeTruthy() expect( validator({ operator: 'equal', @@ -692,8 +1164,8 @@ describe('validator', () => { [ ['MA47BXTJLl0Uf6dsw3hcI', 'u-980ifeIW4HcE6JgUl5s'] ], - filed)).toBeFalsy() - filed.options.multiple = false + field)).toBeFalsy() + field.options.multiple = false expect( validator({ operator: 'not_equal', @@ -706,7 +1178,7 @@ describe('validator', () => { 'MA47BXTJLl0Uf6dsw3hcI', '132RPqxAbuGTp-ZW-_Lav' ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', @@ -718,8 +1190,8 @@ describe('validator', () => { [ 'MA47BXTJLl0Uf6dsw3hcI' ], - filed)).toBeTruthy() - filed.options.multiple = true + field)).toBeTruthy() + field.options.multiple = true expect( validator({ operator: 'not_equal', @@ -732,7 +1204,7 @@ describe('validator', () => { ['MA47BXTJLl0Uf6dsw3hcI', 'u-980ifeIW4HcE6JgUl5s'], ['MA47BXTJLl0Uf6dsw3hcI', '132RPqxAbuGTp-ZW-_Lav'] ], - filed)).toBeFalsy() + field)).toBeFalsy() expect( validator({ operator: 'not_equal', @@ -744,26 +1216,96 @@ describe('validator', () => { [ ['MA47BXTJLl0Uf6dsw3hcI', 'u-980ifeIW4HcE6JgUl5s'] ], - filed)).toBeTruthy() + field)).toBeTruthy() + }) + test('File', () => { + const field = { type: 'uploadfile', label: 'File', icon: 'upload', key: 'uploadfile_FSDpM3CGDUP7-GioNkFLn', id: 'FSDpM3CGDUP7-GioNkFLn', options: { isShowLabel: true, labelWidth: 100, defaultValue: [], multiple: false, action: 'http://localhost:8001/Everright-api/lowCode/uploads', limit: 1, size: 1, accept: '.png,.jpg,.gif', disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + [], + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + [], + field)).toBeFalsy() + }) + test('Signature', () => { + const field = { type: 'signature', label: 'Signature', icon: 'signature', key: 'signature_TBB-T_V2Gkt-BwinGdG4f', id: 'TBB-T_V2Gkt-BwinGdG4f', options: { required: false, isShowLabel: true, labelWidth: 100, defaultValue: '', penColor: 'rgb(0, 0, 0)', action: 'http://localhost:8001/Everright-api/lowCode/uploads', disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() }) - // test('File', () => { - // const filed = { type: 'uploadfile', label: 'File', icon: 'upload', key: 'uploadfile_FSDpM3CGDUP7-GioNkFLn', id: 'FSDpM3CGDUP7-GioNkFLn', options: { isShowLabel: true, labelWidth: 100, defaultValue: [], multiple: false, action: 'http://localhost:8001/Everright-api/lowCode/uploads', limit: 1, size: 1, accept: '.png,.jpg,.gif', disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } - // expect( - // validator({ - // operator: 'equal', - // value: '123
' - // }, - // '123
', - // filed)).toBeTruthy() - // }) - // test('Signature', () => { - // const filed = { type: 'signature', label: 'Signature', icon: 'signature', key: 'signature_TBB-T_V2Gkt-BwinGdG4f', id: 'TBB-T_V2Gkt-BwinGdG4f', options: { required: false, isShowLabel: true, labelWidth: 100, defaultValue: '', penColor: 'rgb(0, 0, 0)', action: 'http://localhost:8001/Everright-api/lowCode/uploads', disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } - // expect( - // validator({ - // operator: 'equal', - // value: '123
' - // }, - // '123
', - // filed)).toBeTruthy() - // }) })