Commit 7d7f9196 authored by nanahira's avatar nanahira

adapt

parent 43c90aa5
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"schemastery": "^2.0.0", "schemastery": "^2.1.0",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, },
"peerDependencies": { "peerDependencies": {
...@@ -1541,9 +1541,9 @@ ...@@ -1541,9 +1541,9 @@
} }
}, },
"node_modules/schemastery": { "node_modules/schemastery": {
"version": "2.0.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.0.0.tgz", "resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.1.0.tgz",
"integrity": "sha512-g628Fvc8cY3Laoyz+v45qZGiClwhTj0MsiGF+tSVlUh+hqYf6kVkTaWdIHNGhfj11h9QzRrs0A4wfvQZuHg3iA==", "integrity": "sha512-D5wGNn5lc8GnhTzSl2ClH6RwUB69NMUrIXn6qWJVZSSq1fJtEodL9zXoJUbzoLQlL7rGMuO6entajhZvJY25Tw==",
"dev": true "dev": true
}, },
"node_modules/semver": { "node_modules/semver": {
...@@ -2932,9 +2932,9 @@ ...@@ -2932,9 +2932,9 @@
} }
}, },
"schemastery": { "schemastery": {
"version": "2.0.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.0.0.tgz", "resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.1.0.tgz",
"integrity": "sha512-g628Fvc8cY3Laoyz+v45qZGiClwhTj0MsiGF+tSVlUh+hqYf6kVkTaWdIHNGhfj11h9QzRrs0A4wfvQZuHg3iA==", "integrity": "sha512-D5wGNn5lc8GnhTzSl2ClH6RwUB69NMUrIXn6qWJVZSSq1fJtEodL9zXoJUbzoLQlL7rGMuO6entajhZvJY25Tw==",
"dev": true "dev": true
}, },
"semver": { "semver": {
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"schemastery": "^2.0.0", "schemastery": "^2.1.0",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, },
"peerDependencies": { "peerDependencies": {
......
...@@ -8,21 +8,21 @@ import { ...@@ -8,21 +8,21 @@ import {
transformSingle, transformSingle,
} from '../utility/transformer'; } from '../utility/transformer';
import Schema from 'schemastery'; import Schema from 'schemastery';
import { kSchema } from '../utility/kschema';
export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator { export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
return (obj, key) => { return (obj, key) => {
const nativeType = Reflect.getMetadata('design:type', obj, key); const nativeType = Reflect.getMetadata('design:type', obj, key);
const nativeTypeString = getStringFromNativeType(nativeType); // const nativeTypeString = getStringFromNativeType(nativeType);
if (!options.type) { if (!options.type) {
if (nativeTypeString && nativeTypeString !== 'array') { if (nativeType !== Array) {
options.type = options.type = nativeType;
nativeTypeString === 'class' ? nativeType : nativeTypeString;
} else { } else {
options.type = 'any'; options.type = 'any';
} }
} }
if ( if (
nativeTypeString === 'array' && nativeType === Array &&
options.array !== false && options.array !== false &&
options.schema?.type !== 'tuple' options.schema?.type !== 'tuple'
) { ) {
...@@ -32,7 +32,7 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator { ...@@ -32,7 +32,7 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
if ( if (
options.type && options.type &&
typeof options.type !== 'string' && typeof options.type !== 'string' &&
typeof (options.type as Schema<any>).type === 'string' (options.type as Schema<any>)[kSchema]
) { ) {
const cl = options.type as ClassType<any>; const cl = options.type as ClassType<any>;
let dec: PropertyDecorator; let dec: PropertyDecorator;
......
...@@ -8,14 +8,12 @@ import Schema from 'schemastery'; ...@@ -8,14 +8,12 @@ import Schema from 'schemastery';
import { reflector } from '../metadata/reflector'; import { reflector } from '../metadata/reflector';
import _ from 'lodash'; import _ from 'lodash';
import { Metadata } from '../metadata/metadata'; import { Metadata } from '../metadata/metadata';
import { kSchema } from '../utility/kschema';
function getBasePropertySchemaFromOptions(options: SchemaOptions) { function getBasePropertySchemaFromOptions(options: SchemaOptions) {
if (options.schema) { if (options.schema) {
return options.schema; return options.schema;
} }
if (typeof options.type !== 'string') {
return schemaFromClass(options.type);
}
switch (options.type as string) { switch (options.type as string) {
case 'any': case 'any':
return Schema.any(); return Schema.any();
...@@ -30,7 +28,7 @@ function getBasePropertySchemaFromOptions(options: SchemaOptions) { ...@@ -30,7 +28,7 @@ function getBasePropertySchemaFromOptions(options: SchemaOptions) {
case 'object': case 'object':
return Schema.object({}).default({}); return Schema.object({}).default({});
default: default:
return Schema.any(); return Schema.from(options.type);
} }
} }
...@@ -89,7 +87,7 @@ export function schemaFromClass<T>(cl: ClassType<T>): Schema<Partial<T>, T> { ...@@ -89,7 +87,7 @@ export function schemaFromClass<T>(cl: ClassType<T>): Schema<Partial<T>, T> {
return schema; return schema;
} }
const schemaFields: (keyof Schema.Base)[] = [ const schemaFields: (keyof Schema)[] = [
'type', 'type',
'inner', 'inner',
'list', 'list',
...@@ -99,6 +97,16 @@ const schemaFields: (keyof Schema.Base)[] = [ ...@@ -99,6 +97,16 @@ const schemaFields: (keyof Schema.Base)[] = [
'meta', 'meta',
]; ];
const schemaFunctions: (keyof Schema)[] = [
'toJSON',
'required',
'hidden',
'adaptive',
'default',
'comment',
'description',
];
function applySchemaForClass<T>( function applySchemaForClass<T>(
originalClass: ClassType<T>, originalClass: ClassType<T>,
instance: T, instance: T,
...@@ -139,6 +147,16 @@ export function SchemaClass<T>(originalClass: ClassType<T>) { ...@@ -139,6 +147,16 @@ export function SchemaClass<T>(originalClass: ClassType<T>) {
// @ts-ignore // @ts-ignore
newClass[field] = schema[field]; newClass[field] = schema[field];
} }
for (const functionField of schemaFunctions) {
if (newClass[functionField]) {
continue;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
newClass[functionField] = Schema.prototype[functionField];
}
newClass[kSchema] = true;
return newClass; return newClass;
} }
......
...@@ -9,6 +9,8 @@ export type SchemaType = ...@@ -9,6 +9,8 @@ export type SchemaType =
| 'object' | 'object'
| 'any' | 'any'
| 'never' | 'never'
// eslint-disable-next-line @typescript-eslint/ban-types
| Function
| { new (...args: any[]): any }; | { new (...args: any[]): any };
export interface SchemaClassOptions extends Schema.Meta<any> { export interface SchemaClassOptions extends Schema.Meta<any> {
......
import Schema from 'schemastery';
function getKSchema(): symbol {
return Object.getOwnPropertySymbols(Schema.prototype).find(
(s) => s.toString() === 'Symbol(schemastery)',
);
}
export const kSchema = getKSchema();
...@@ -2,6 +2,7 @@ import { Metadata } from '../metadata/metadata'; ...@@ -2,6 +2,7 @@ import { Metadata } from '../metadata/metadata';
import { ClassType } from '../def'; import { ClassType } from '../def';
import Schema from 'schemastery'; import Schema from 'schemastery';
import _ from 'lodash'; import _ from 'lodash';
import { kSchema } from './kschema';
export function SetTransformer(transformer: (val: any) => any) { export function SetTransformer(transformer: (val: any) => any) {
return Metadata.set('Transformer', transformer); return Metadata.set('Transformer', transformer);
...@@ -11,7 +12,7 @@ export function transformSingle<T>( ...@@ -11,7 +12,7 @@ export function transformSingle<T>(
cl: ClassType<T> & Partial<Schema>, cl: ClassType<T> & Partial<Schema>,
val: any, val: any,
) { ) {
if (typeof cl.type === 'string') { if (cl[kSchema]) {
return new cl(val); return new cl(val);
} else { } else {
return val; return val;
......
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