Skip to content

Commit

Permalink
feat(vae): 添加 cuid, cuid2
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 8, 2023
1 parent c71997a commit da62ff3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/vae/VaeLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export type VaeLocaleShape = {
| 'startsWith'
| 'endsWith'
| 'phoneNumber'
| 'idCardNumber',
| 'idCardNumber'
| 'cuid'
| 'cuid2',
VaeLocaleMessage
>
object: Record<'type' | 'requiredFieldsAtLeastOne', VaeLocaleMessage>
Expand Down Expand Up @@ -92,6 +94,8 @@ export class VaeLocaleBuilder {
`${options.getLabel(payload)}应是一个合法的手机号码`,
idCardNumber: payload =>
`${options.getLabel(payload)}应是一个合法的身份证号码`,
cuid: payload => `${options.getLabel(payload)}有误`,
cuid2: payload => `${options.getLabel(payload)}有误`,
},

object: {
Expand Down
18 changes: 18 additions & 0 deletions src/vae/VaeStringSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
endsWith,
includes,
isChineseIDCardNumber,
isCuid,
isCuid2,
isEmail,
isPossibleChineseMobilePhoneNumber,
isString,
Expand Down Expand Up @@ -148,6 +150,22 @@ export class VaeStringSchema<
})
}

cuid(message: VaeLocaleMessage = VaeLocale.string.cuid) {
return this.check({
fn: isCuid,
message: message,
tag: 'cuid',
})
}

cuid2(message: VaeLocaleMessage = VaeLocale.string.cuid2) {
return this.check({
fn: isCuid2,
message: message,
tag: 'cuid2',
})
}

trim() {
this._options.stringTrim = true
return this
Expand Down
44 changes: 44 additions & 0 deletions src/vae/__snapshots__/vae.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,50 @@ Object {
}
`;

exports[`vae cuid, cuid2 1`] = `
Object {
"issues": Array [
Object {
"message": "cuid有误",
"path": Array [
"cuid",
],
},
Object {
"message": "cuid2有误",
"path": Array [
"cuid2",
],
},
],
"success": false,
}
`;

exports[`vae cuid, cuid2 2`] = `
Object {
"issues": Array [
Object {
"message": "cuid2有误",
"path": Array [
"cuid2",
],
},
],
"success": false,
}
`;

exports[`vae cuid, cuid2 3`] = `
Object {
"data": Object {
"cuid": "clpw4etzi0000sgrl4uki28fv",
"cuid2": "tz4a98xxat96iws9zmbrgj3a",
},
"success": true,
}
`;

exports[`vae date 1`] = `
Object {
"data": undefined,
Expand Down
24 changes: 23 additions & 1 deletion src/vae/vae.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequiredDeep } from '../types'
import { VaeSchemaOf, v } from './vae'
import { VaeObjectSchema, VaeSchemaOf, v } from './vae'

describe('vae', () => {
test('string', () => {
Expand Down Expand Up @@ -985,4 +985,26 @@ describe('vae', () => {
),
).toMatchSnapshot()
})

test('cuid, cuid2', () => {
const schema: VaeObjectSchema<{
cuid: string
cuid2: string
}> = v.create(_ =>
_.object({
cuid: _.string().cuid(),
cuid2: _.string().cuid2(),
}),
)
expect(schema.parse({ cuid: '1', cuid2: 'xxxxxx' })).toMatchSnapshot()
expect(
schema.parse({ cuid: 'clpw4etzi0000sgrl4uki28fv', cuid2: 'xxxxxx' }),
).toMatchSnapshot()
expect(
schema.parse({
cuid: 'clpw4etzi0000sgrl4uki28fv',
cuid2: 'tz4a98xxat96iws9zmbrgj3a',
}),
).toMatchSnapshot()
})
})

0 comments on commit da62ff3

Please sign in to comment.