Skip to content

Commit

Permalink
fix validator of id card to support tail x (#227)
Browse files Browse the repository at this point in the history
* fix: revert import antd/next on demand

* fix(@uform/validator): id card supports tail x (#219)

* test(validate): add idcard rule test
  • Loading branch information
anyuxuan authored and janryWang committed Aug 5, 2019
1 parent e55d840 commit 33291e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion packages/react/src/__tests__/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import SchemaForm, {
FormPath,
createFormActions
} from '../index'
import { render, fireEvent, act } from '@testing-library/react'
import {
render,
fireEvent,
act,
waitForElement,
waitForDomChange
} from '@testing-library/react'

registerFieldMiddleware(Field => {
return props => {
Expand Down Expand Up @@ -265,3 +271,43 @@ test('dynamic update values', async () => {
await sleep(33)
expect(queryByText('must be number')).toBeVisible()
})

test('test idcard rules', async () => {
const TestComponent = () => {
return (
<SchemaForm>
<Field name="idCard" type="string" x-rules="idcard" />
</SchemaForm>
)
}
const { queryByTestId, queryByText } = render(<TestComponent />)
// 14位数字
const value1 = '12345678912345'
// 15位数字
const value2 = '123456789123456'
// 17位数字
const value3 = '12345678912345678'
// 17位数字+x
const value4 = '12345678912345678x'
// 17位数字+X
const value5 = '12345678912345678X'
// 18位数字
const value6 = '123456789123456789'
const element = await waitForElement(() => queryByTestId('test-input'))
waitForDomChange({ container: element }).then(mutationsList => {
const mutation = mutationsList[0]
const { value } = mutation.target
const errorTipsElement = queryByText('idCard is not an idcard format')
if (value === value1 || value === value3) {
errorTipsElement.toBeVisible()
} else {
errorTipsElement.toBeNull()
}
})
fireEvent.change(element, { target: { value: value1 } })
fireEvent.change(element, { target: { value: value2 } })
fireEvent.change(element, { target: { value: value3 } })
fireEvent.change(element, { target: { value: value4 } })
fireEvent.change(element, { target: { value: value5 } })
fireEvent.change(element, { target: { value: value6 } })
})
2 changes: 1 addition & 1 deletion packages/validator/src/validators/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default {

phone: /^\d{3}-\d{8}$|^\d{4}-\d{7}$|^\d{11}$/,

idcard: /^\d{15}$|^\d{18}$/,
idcard: /^\d{15}$|^\d{17}(\d|x|X)$/,

taodomain: /^(https?\:)?\/\/[a-zA-Z0-9\.\-]+\.(taobao|tmall|alitrip|yao\.95095)(\.daily)?\.(com|net|hk(\/hk)?)/,

Expand Down

0 comments on commit 33291e3

Please sign in to comment.