Commit f4e8bd77 authored by nanahira's avatar nanahira

Merge branch 'master' into feat-backref

parents 9f2bf284 e283d896
{ {
"name": "schemastery-gen", "name": "schemastery-gen",
"version": "2.3.1", "version": "3.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "schemastery-gen", "name": "schemastery-gen",
"version": "2.3.1", "version": "3.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
...@@ -25,12 +25,11 @@ ...@@ -25,12 +25,11 @@
"jest": "^27.4.3", "jest": "^27.4.3",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"schemastery": "^2.1.2",
"ts-jest": "^27.0.7", "ts-jest": "^27.0.7",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, },
"peerDependencies": { "peerDependencies": {
"schemastery": "^2.1.1" "schemastery": "^3.0.0"
} }
}, },
"node_modules/@babel/code-frame": { "node_modules/@babel/code-frame": {
...@@ -4546,10 +4545,10 @@ ...@@ -4546,10 +4545,10 @@
} }
}, },
"node_modules/schemastery": { "node_modules/schemastery": {
"version": "2.1.2", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.1.2.tgz", "resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.0.0.tgz",
"integrity": "sha512-iHwWfKxWaTFgZmKNULNtgyo8VDpdEWx31b6+j0tGTST8dBIYU7VYAHmq5qdYKYZ0uNSM5u57c09cuN75Yf7WwQ==", "integrity": "sha512-0aWxVyVoa5XMxfsFqHdyj2QZCiUgImzcsCEdseALm57JlmBktu92EvTXm1AebD21q/fcSi+M7BGclDQyfMjQzg==",
"dev": true "peer": true
}, },
"node_modules/semver": { "node_modules/semver": {
"version": "7.3.5", "version": "7.3.5",
...@@ -8712,10 +8711,10 @@ ...@@ -8712,10 +8711,10 @@
} }
}, },
"schemastery": { "schemastery": {
"version": "2.1.2", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-2.1.2.tgz", "resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.0.0.tgz",
"integrity": "sha512-iHwWfKxWaTFgZmKNULNtgyo8VDpdEWx31b6+j0tGTST8dBIYU7VYAHmq5qdYKYZ0uNSM5u57c09cuN75Yf7WwQ==", "integrity": "sha512-0aWxVyVoa5XMxfsFqHdyj2QZCiUgImzcsCEdseALm57JlmBktu92EvTXm1AebD21q/fcSi+M7BGclDQyfMjQzg==",
"dev": true "peer": true
}, },
"semver": { "semver": {
"version": "7.3.5", "version": "7.3.5",
......
{ {
"name": "schemastery-gen", "name": "schemastery-gen",
"description": "Decorator style of [Schemastery](https://github.com/Shigma/schemastery)", "description": "Decorator style of [Schemastery](https://github.com/Shigma/schemastery)",
"version": "2.3.1", "version": "3.0.0",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"scripts": { "scripts": {
...@@ -33,13 +33,9 @@ ...@@ -33,13 +33,9 @@
"jest": "^27.4.3", "jest": "^27.4.3",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"schemastery": "^2.1.2",
"ts-jest": "^27.0.7", "ts-jest": "^27.0.7",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, },
"peerDependencies": {
"schemastery": "^2.1.2"
},
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
...@@ -61,5 +57,8 @@ ...@@ -61,5 +57,8 @@
], ],
"coverageDirectory": "../coverage", "coverageDirectory": "../coverage",
"testEnvironment": "node" "testEnvironment": "node"
},
"peerDependencies": {
"schemastery": "^3.0.0"
} }
} }
import { SchemaClassOptions, SchemaOptions, SchemaProperty } from '../def';
import { RegisterSchema } from './register';
export const DefineSchema =
(
options: SchemaOptions | SchemaClassOptions = {},
): PropertyDecorator & ClassDecorator =>
(obj, key?) => {
if (key) {
return SchemaProperty(options)(obj, key);
} else {
return RegisterSchema(options)(obj);
}
};
export const SchemaConf = RegisterSchema;
...@@ -4,7 +4,8 @@ import { ...@@ -4,7 +4,8 @@ import {
SchemaOptions, SchemaOptions,
SchemaReference, SchemaReference,
SchemaType, SchemaType,
} from '../def/interfaces'; } from '../def';
import { Metadata } from '../metadata/metadata'; import { Metadata } from '../metadata/metadata';
import { import {
SetTransformer, SetTransformer,
...@@ -59,9 +60,6 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator { ...@@ -59,9 +60,6 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
}; };
} }
// for backward compatibility
export const DefineSchema = SchemaProperty;
export function SchemaRef<S = any, T = S>( export function SchemaRef<S = any, T = S>(
factory: () => SchemaType<S, T>, factory: () => SchemaType<S, T>,
): SchemaReference<S, T> { ): SchemaReference<S, T> {
......
export * from './define-property'; export * from './define-property';
export * from './register'; export * from './register';
export * from './compat';
...@@ -18,19 +18,25 @@ function resolveSchemaType(schemaType: SchemaType): SchemaOrReference { ...@@ -18,19 +18,25 @@ function resolveSchemaType(schemaType: SchemaType): SchemaOrReference {
if (schemaType && schemaType[RefSym]) { if (schemaType && schemaType[RefSym]) {
return schemaType as SchemaReference; return schemaType as SchemaReference;
} }
switch (schemaType as string) { // eslint-disable-next-line @typescript-eslint/ban-types
switch (schemaType as string | Function) {
case 'any': case 'any':
return Schema.any(); return Schema.any();
case 'never': case 'never':
return Schema.never(); return Schema.never();
case 'string': case 'string':
case String:
return Schema.string(); return Schema.string();
case 'number': case 'number':
case Number:
return Schema.number(); return Schema.number();
case 'boolean': case 'boolean':
case Boolean:
return Schema.boolean(); return Schema.boolean();
case 'object': case 'object':
return Schema.object({}).default({}); return Schema.object({}).default({});
case Function:
return Schema.function();
default: default:
return Schema.from(schemaType); return Schema.from(schemaType);
} }
...@@ -130,6 +136,9 @@ function schemaOptionsFromClass<T>(cl: ClassType<T>): SchemaOptionsDict<T> { ...@@ -130,6 +136,9 @@ function schemaOptionsFromClass<T>(cl: ClassType<T>): SchemaOptionsDict<T> {
const result: SchemaOptionsDict<T> = {}; const result: SchemaOptionsDict<T> = {};
for (const key of keys) { for (const key of keys) {
const option = reflector.get('SchemaMeta', cl, key); const option = reflector.get('SchemaMeta', cl, key);
if (!option) {
continue;
}
result[key] = option; result[key] = option;
} }
return result; return result;
...@@ -163,13 +172,17 @@ const schemaFields: (keyof Schema)[] = [ ...@@ -163,13 +172,17 @@ const schemaFields: (keyof Schema)[] = [
const schemaFunctions: (keyof Schema)[] = [ const schemaFunctions: (keyof Schema)[] = [
'toJSON', 'toJSON',
// 'toString', 'toBSON',
'required', 'required',
'hidden', 'hidden',
'adaptive', 'role',
'link',
'default', 'default',
'comment', 'comment',
'description', 'description',
'max',
'min',
'step',
]; ];
function applySchemaForClass<T>( function applySchemaForClass<T>(
...@@ -238,6 +251,3 @@ export function RegisterSchema(options: SchemaClassOptions = {}) { ...@@ -238,6 +251,3 @@ export function RegisterSchema(options: SchemaClassOptions = {}) {
return SchemaClass(originalClass); return SchemaClass(originalClass);
}; };
} }
// for backward compatibility
export const SchemaConf = RegisterSchema;
import { RegisterSchema, SchemaProperty } from '../src/decorators'; import {
DefineSchema,
RegisterSchema,
SchemaProperty,
} from '../src/decorators';
import Schema from 'schemastery'; import Schema from 'schemastery';
describe('Schema arrays', () => { describe('Schema arrays', () => {
@RegisterSchema() @DefineSchema()
class MyProperty { class MyProperty {
@SchemaProperty({ default: 'default' }) @DefineSchema({ default: 'default' })
name: string; name: string;
getName() { getName() {
...@@ -16,7 +20,7 @@ describe('Schema arrays', () => { ...@@ -16,7 +20,7 @@ describe('Schema arrays', () => {
class MyConfig { class MyConfig {
constructor(_: any) {} constructor(_: any) {}
@SchemaProperty({ type: Schema.array(Schema.string()) }) @SchemaProperty({ type: Schema.array(Schema.string()), default: ['dress'] })
foo: string[]; foo: string[];
@SchemaProperty({ type: Number }) @SchemaProperty({ type: Number })
...@@ -28,10 +32,13 @@ describe('Schema arrays', () => { ...@@ -28,10 +32,13 @@ describe('Schema arrays', () => {
@SchemaProperty() @SchemaProperty()
notArray: string; notArray: string;
@SchemaProperty({ type: Schema.tuple([Number, String]) }) @SchemaProperty({ type: Schema.tuple([Schema.number(), Schema.string()]) })
myTup: [number, string]; myTup: [number, string];
@SchemaProperty({ type: Schema.tuple([Number, String]), array: true }) @SchemaProperty({
type: Schema.tuple([Schema.number(), Schema.string()]),
array: true,
})
myTupArr: [number, string][]; myTupArr: [number, string][];
} }
...@@ -47,7 +54,12 @@ describe('Schema arrays', () => { ...@@ -47,7 +54,12 @@ describe('Schema arrays', () => {
}); });
it('should be instance of property', () => { it('should be instance of property', () => {
const config = new MyConfig({ myProperties: [{ name: 'foo' }] }); const config = new MyConfig({
myProperties: [{ name: 'foo' }],
bar: [4],
});
expect(config.myProperties[0].getName()).toEqual('foo'); expect(config.myProperties[0].getName()).toEqual('foo');
expect(config.bar[0]).toEqual(4);
expect(config.foo[0]).toEqual('dress');
}); });
}); });
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