Skip to content

Commit

Permalink
feat(vae): 新增 meta 方法设置一些额外的元数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 21, 2023
1 parent ca21a6b commit 9f77a04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/vae/VaeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export type VaeSchemaType =
| 'boolean'
| 'array'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface VaeSchemaCustomMetadata {}
export type VaeSchemaMetadata = keyof VaeSchemaCustomMetadata extends never
? Record<any, any>
: VaeSchemaCustomMetadata

export type VaeSchemaOptions<T, S> = {
type: VaeSchemaType
label?: string
Expand All @@ -51,6 +57,7 @@ export type VaeSchemaOptions<T, S> = {
stringEmptyable?: boolean
processors?: Array<VaeSchemaCheckPayload<T> | VaeSchemaTransformPayload<T>>
runtime?: VaeSchemaRuntimeFn<T, S>
metadata?: VaeSchemaMetadata
}

export type VaeSchemaPath = Array<string | number>
Expand Down Expand Up @@ -153,6 +160,11 @@ export abstract class VaeSchema<T extends any = any> {
return this._options
}

meta(metadata: VaeSchemaMetadata) {
this._options.metadata = metadata
return this
}

check(payload: VaeSchemaCheckPayload<T>) {
const index = payload.tag
? findIndex(
Expand Down
7 changes: 7 additions & 0 deletions src/vae/__snapshots__/vae.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ Object {
}
`;

exports[`vae meta 1`] = `
Object {
"x": 1,
"y": "222",
}
`;

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

test('meta', () => {
expect(
v.string().min(3).meta({ x: 1, y: '222' }).options.metadata,
).toMatchSnapshot()
})
})

0 comments on commit 9f77a04

Please sign in to comment.