Skip to content

Commit

Permalink
feat(vae): object 新增 shapeOfFields 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 27, 2023
1 parent e978811 commit 7b0a95a
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/vae/VaeObjectSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Nullable, PartialBy, RequiredBy } from '../types'
import { difference, intersection, isPlainObject, startsWith } from '../utils'
import {
difference,
includes,
intersection,
isPlainObject,
startsWith,
} from '../utils'
import { VaeLocale, VaeLocaleMessage } from './VaeLocale'
import { VaeSchema, VaeSchemaOf } from './VaeSchema'

Expand Down Expand Up @@ -46,7 +52,7 @@ export class VaeObjectSchema<
pickFields<K extends keyof T>(keys: K[]): VaeObjectSchema<Pick<T, K>> {
this._options.processors = this._options.processors.filter(item =>
typeof item === 'object' && startsWith(item.tag, 'field_')
? (keys as any).includes(item.path![0])
? includes(keys as any, item.path![0])
: true,
)
this._options.objectKeys = intersection(
Expand All @@ -59,7 +65,7 @@ export class VaeObjectSchema<
omitFields<K extends keyof T>(keys: K[]): VaeObjectSchema<Omit<T, K>> {
this._options.processors = this._options.processors.filter(item =>
typeof item === 'object' && startsWith(item.tag, 'field_')
? !(keys as any).includes(item.path![0])
? !includes(keys as any, item.path![0])
: true,
)
this._options.objectKeys = difference(this._options.objectKeys, keys as any)
Expand All @@ -73,7 +79,7 @@ export class VaeObjectSchema<
if (
typeof item === 'object' &&
startsWith(item.tag, 'field_') &&
(keys ? (keys as any).includes(item.path![0]) : true)
(keys ? includes(keys as any, item.path![0]) : true)
) {
;(item.fn as VaeSchema).optional()
}
Expand All @@ -90,11 +96,29 @@ export class VaeObjectSchema<
if (
typeof item === 'object' &&
startsWith(item.tag, 'field_') &&
(keys ? (keys as any).includes(item.path![0]) : true)
(keys ? includes(keys as any, item.path![0]) : true)
) {
;(item.fn as VaeSchema).required()
}
})
return this
}

shapeOfFields<K extends keyof T>(
keys: K[],
): VaeObjectSchemaShapeOf<Pick<T, K>>
shapeOfFields(): VaeObjectSchemaShapeOf<T>
shapeOfFields(keys?: string[]) {
const shape: Record<any, any> = {}
this._options.processors.forEach(item => {
if (
typeof item === 'object' &&
startsWith(item.tag, 'field_') &&
(keys ? includes(keys as any, item.path![0]) : true)
) {
shape[item.path![0]] = item.fn
}
})
return shape
}
}
53 changes: 53 additions & 0 deletions src/vae/__snapshots__/vae.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,59 @@ Object {
}
`;

exports[`vae shapeOfFields 1`] = `
Object {
"data": Object {
"campus": undefined,
"class": undefined,
"college": undefined,
"companyName": undefined,
"gender": "male",
"grade": undefined,
"idNumber": undefined,
"identityId": 1,
"major": undefined,
"name": "成龙",
"school": undefined,
"studentNumber": undefined,
"userId": 1,
},
"success": true,
}
`;

exports[`vae shapeOfFields 2`] = `
Object {
"data": Object {
"gender": "male",
"id": 1,
"name": "成龙",
},
"success": true,
}
`;

exports[`vae shapeOfFields 3`] = `
Object {
"data": Object {
"campus": undefined,
"class": undefined,
"college": undefined,
"companyName": undefined,
"gender": "male",
"grade": undefined,
"idNumber": undefined,
"identityId": 1,
"major": undefined,
"name": "成龙",
"school": undefined,
"studentNumber": undefined,
"userId": 1,
},
"success": true,
}
`;

exports[`vae string 1`] = `
Object {
"data": undefined,
Expand Down
68 changes: 68 additions & 0 deletions src/vae/vae.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,72 @@ describe('vae', () => {
}),
).toMatchSnapshot()
})

test('shapeOfFields', () => {
type Data = {
userId: number
gender?: 'male' | 'female' | 'unknown'
name: string
campus?: string
college?: string
identityId: number
grade?: number
school?: string
major?: string
class?: string
studentNumber?: string
idNumber?: string
companyName?: string
}
const _ = v
const schema: VaeSchemaOf<Data> = _.object({
userId: _.number().required().id(),
gender: _.string(),
name: _.string().required().max(20),
campus: _.string().max(20),
college: _.string().max(20),
identityId: _.number().required().id(),
grade: _.number().positiveInteger(),
school: _.string().max(20),
major: _.string().max(20),
class: _.string().max(20),
studentNumber: _.string().max(20),
idNumber: _.string().max(18).idCardNumber(),
companyName: _.string().max(50),
})
expect(
schema.parse({
name: '成龙',
identityId: 1,
userId: 1,
gender: 'male',
}),
).toMatchSnapshot()

const schema2: VaeSchemaOf<Pick<Data, 'name' | 'gender'> & { id: number }> =
_.object({
id: _.number().required(),
...schema.shapeOfFields(['name', 'gender']),
})
expect(
schema2.parse({
id: 1,
name: '成龙',
// @ts-expect-error
identityId: 1,
userId: 1,
gender: 'male',
}),
).toMatchSnapshot()

const schema3: VaeSchemaOf<Data> = _.object(schema.shapeOfFields())
expect(
schema3.parse({
name: '成龙',
identityId: 1,
userId: 1,
gender: 'male',
}),
).toMatchSnapshot()
})
})

0 comments on commit 7b0a95a

Please sign in to comment.