Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types/schema-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface BaseSchema {
}

// 根据type字段动态选择验证属性
type TypeSpecificSchema<T extends JsonSchemaType> = {
export type TypeSpecificSchema<T extends JsonSchemaType> = {
[K in T]: ValidationMap[K];
}[T];

Expand Down
24 changes: 24 additions & 0 deletions src/utils/module-extends-options/router-schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import type { TypeSpecificSchema } from '../../types/schema-type';
import type { AppRouterOptions, GetExtendsOptions } from './type';

/** string 类型 schema */
const stringType = { type: 'string' } as const;
/** number 类型 schema */
const numberType = { type: 'number' } as const;
/** boolean 类型 schema */
const booleanType = { type: 'boolean' } as const;
/** 获取 array 类型 schema */
function getArrayType(items: TypeSpecificSchema<'array'>['items'], other?: Omit<TypeSpecificSchema<'array'>, 'items'>) {
return { type: 'array', items, ...other } as const;
}
/** 获取 object 类型 schema */
function getObjectType<O extends TypeSpecificSchema<'object'>['properties']>(properties: O, required?: (keyof O)[], other?: Omit<TypeSpecificSchema<'object'>, 'properties' | 'required'>) {
if (!required && typeof properties === 'string' && properties !== null) {
required = Object.keys(properties as any) as any;
}
return { type: 'object', properties, required, ...other } as const;
}

/**
* router-schema 模块加载时的额外入参, 用于提供辅助方法等
*/
export function getRouterSchemaExtendsOptions(_: AppRouterOptions) {
const handler = {
prefix: '',
stringType,
numberType,
booleanType,
getArrayType,
getObjectType,
setPrefix(prefix: string) {
this.prefix = prefix;
},
Expand Down