Commit 63d729e6 authored by nanahira's avatar nanahira

rework dict

parent 7d7f9196
...@@ -21,10 +21,12 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator { ...@@ -21,10 +21,12 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
options.type = 'any'; options.type = 'any';
} }
} }
const typeInSchema = options.schema?.type;
if ( if (
nativeType === Array && nativeType === Array &&
options.array !== false && options.array !== false &&
options.schema?.type !== 'tuple' typeInSchema !== 'array' &&
typeInSchema !== 'tuple'
) { ) {
options.array = true; options.array = true;
} }
......
...@@ -42,7 +42,13 @@ function applyOptionsToSchema(schema: Schema, options: SchemaClassOptions) { ...@@ -42,7 +42,13 @@ function applyOptionsToSchema(schema: Schema, options: SchemaClassOptions) {
function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> { function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> {
let schema = getBasePropertySchemaFromOptions(options); let schema = getBasePropertySchemaFromOptions(options);
if (options.dict) { 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) { if (options.array) {
schema = Schema.array(schema).default([]); schema = Schema.array(schema).default([]);
...@@ -89,6 +95,7 @@ export function schemaFromClass<T>(cl: ClassType<T>): Schema<Partial<T>, T> { ...@@ -89,6 +95,7 @@ export function schemaFromClass<T>(cl: ClassType<T>): Schema<Partial<T>, T> {
const schemaFields: (keyof Schema)[] = [ const schemaFields: (keyof Schema)[] = [
'type', 'type',
'sKey',
'inner', 'inner',
'list', 'list',
'dict', 'dict',
......
...@@ -2,17 +2,27 @@ import Schema from 'schemastery'; ...@@ -2,17 +2,27 @@ import Schema from 'schemastery';
export type ClassType<T> = { new (...args: any[]): T }; export type ClassType<T> = { new (...args: any[]): T };
export type SchemaType = export type SchemaNativeType =
| 'string' | 'string'
| 'number' | 'number'
| 'boolean' | 'boolean'
| 'object' | 'object'
| 'any' | 'any'
| 'never' | 'never';
export type SchemaSource =
| null
| undefined
| string
| number
| boolean
| Schema
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
| Function | Function
| { new (...args: any[]): any }; | { new (...args: any[]): any };
export type SchemaType = SchemaNativeType | SchemaSource;
export interface SchemaClassOptions extends Schema.Meta<any> { export interface SchemaClassOptions extends Schema.Meta<any> {
desc?: string; desc?: string;
allowUnknown?: boolean; // for backward compatibility allowUnknown?: boolean; // for backward compatibility
...@@ -20,7 +30,7 @@ export interface SchemaClassOptions extends Schema.Meta<any> { ...@@ -20,7 +30,7 @@ export interface SchemaClassOptions extends Schema.Meta<any> {
export interface SchemaOptions extends SchemaClassOptions { export interface SchemaOptions extends SchemaClassOptions {
schema?: Schema<any>; schema?: Schema<any>;
dict?: boolean; dict?: boolean | Schema<any, string> | string;
array?: boolean; array?: boolean;
type?: SchemaType; 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