Commit f4e8bd77 authored by nanahira's avatar nanahira

Merge branch 'master' into feat-backref

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