Commit 63d729e6 authored by nanahira's avatar nanahira

rework dict

parent 7d7f9196
......@@ -21,10 +21,12 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
options.type = 'any';
}
}
const typeInSchema = options.schema?.type;
if (
nativeType === Array &&
options.array !== false &&
options.schema?.type !== 'tuple'
typeInSchema !== 'array' &&
typeInSchema !== 'tuple'
) {
options.array = true;
}
......
......@@ -42,7 +42,13 @@ function applyOptionsToSchema(schema: Schema, options: SchemaClassOptions) {
function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> {
let schema = getBasePropertySchemaFromOptions(options);
if (options.dict) {
schema = Schema.dict(schema).default({});
const skeySchema = (options.dict === true
? Schema.string()
: ((Schema.from(options.dict) as unknown) as Schema<
any,
string
>)) as Schema<any, string>;
schema = Schema.dict(schema, skeySchema).default({});
}
if (options.array) {
schema = Schema.array(schema).default([]);
......@@ -89,6 +95,7 @@ export function schemaFromClass<T>(cl: ClassType<T>): Schema<Partial<T>, T> {
const schemaFields: (keyof Schema)[] = [
'type',
'sKey',
'inner',
'list',
'dict',
......
......@@ -2,17 +2,27 @@ import Schema from 'schemastery';
export type ClassType<T> = { new (...args: any[]): T };
export type SchemaType =
export type SchemaNativeType =
| 'string'
| 'number'
| 'boolean'
| 'object'
| 'any'
| 'never'
| 'never';
export type SchemaSource =
| null
| undefined
| string
| number
| boolean
| Schema
// eslint-disable-next-line @typescript-eslint/ban-types
| Function
| { new (...args: any[]): any };
export type SchemaType = SchemaNativeType | SchemaSource;
export interface SchemaClassOptions extends Schema.Meta<any> {
desc?: string;
allowUnknown?: boolean; // for backward compatibility
......@@ -20,7 +30,7 @@ export interface SchemaClassOptions extends Schema.Meta<any> {
export interface SchemaOptions extends SchemaClassOptions {
schema?: Schema<any>;
dict?: boolean;
dict?: boolean | Schema<any, string> | string;
array?: boolean;
type?: SchemaType;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment