@orion-js/typed-model 3.11.8 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +6776 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +6748 -0
- package/package.json +24 -22
- package/LICENSE +0 -21
- package/lib/decorators/index.d.ts +0 -4
- package/lib/decorators/index.js +0 -16
- package/lib/decorators/prop.d.ts +0 -8
- package/lib/decorators/prop.js +0 -30
- package/lib/decorators/prop.test.d.ts +0 -1
- package/lib/decorators/prop.test.js +0 -26
- package/lib/decorators/resolver.d.ts +0 -5
- package/lib/decorators/resolver.js +0 -14
- package/lib/decorators/typedModel.d.ts +0 -5
- package/lib/decorators/typedModel.js +0 -16
- package/lib/decorators/typedSchema.d.ts +0 -2
- package/lib/decorators/typedSchema.js +0 -13
- package/lib/enum.test.d.ts +0 -1
- package/lib/enum.test.js +0 -29
- package/lib/errors/CannotDetermineType.d.ts +0 -3
- package/lib/errors/CannotDetermineType.js +0 -9
- package/lib/errors/CannotUseArray.d.ts +0 -3
- package/lib/errors/CannotUseArray.js +0 -9
- package/lib/errors/PropertyAlreadyExists.d.ts +0 -3
- package/lib/errors/PropertyAlreadyExists.js +0 -9
- package/lib/errors/SchemaAlreadyExists.d.ts +0 -3
- package/lib/errors/SchemaAlreadyExists.js +0 -9
- package/lib/errors/index.d.ts +0 -4
- package/lib/errors/index.js +0 -16
- package/lib/factories/cloneSchemaClass.d.ts +0 -23
- package/lib/factories/cloneSchemaClass.js +0 -28
- package/lib/factories/cloneSchemaClass.test.d.ts +0 -1
- package/lib/factories/cloneSchemaClass.test.js +0 -38
- package/lib/factories/getModelForClass.d.ts +0 -3
- package/lib/factories/getModelForClass.js +0 -62
- package/lib/factories/getModelForClass.test.d.ts +0 -1
- package/lib/factories/getModelForClass.test.js +0 -59
- package/lib/factories/getSchemaForClass.d.ts +0 -3
- package/lib/factories/getSchemaForClass.js +0 -8
- package/lib/factories/helpers/processSchemaForProp.d.ts +0 -4
- package/lib/factories/helpers/processSchemaForProp.js +0 -65
- package/lib/factories/index.d.ts +0 -3
- package/lib/factories/index.js +0 -15
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -14
- package/lib/index.test.d.ts +0 -1
- package/lib/index.test.js +0 -682
- package/lib/schema.test.d.ts +0 -1
- package/lib/schema.test.js +0 -94
- package/lib/storage/metadataStorage.d.ts +0 -28
- package/lib/storage/metadataStorage.js +0 -55
- package/lib/utils/interfaces.d.ts +0 -3
- package/lib/utils/interfaces.js +0 -2
- package/lib/utils/isClass.d.ts +0 -1
- package/lib/utils/isClass.js +0 -6
package/lib/schema.test.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const models_1 = require("@orion-js/models");
|
|
13
|
-
const schema_1 = require("@orion-js/schema");
|
|
14
|
-
const _1 = require(".");
|
|
15
|
-
describe('Test typed model with Schema', () => {
|
|
16
|
-
it('Should allow passing a typed model to schema', async () => {
|
|
17
|
-
let Point = class Point {
|
|
18
|
-
};
|
|
19
|
-
__decorate([
|
|
20
|
-
(0, _1.Prop)(),
|
|
21
|
-
__metadata("design:type", Number)
|
|
22
|
-
], Point.prototype, "latitude", void 0);
|
|
23
|
-
__decorate([
|
|
24
|
-
(0, _1.Prop)(),
|
|
25
|
-
__metadata("design:type", Number)
|
|
26
|
-
], Point.prototype, "longitude", void 0);
|
|
27
|
-
Point = __decorate([
|
|
28
|
-
(0, _1.TypedModel)()
|
|
29
|
-
], Point);
|
|
30
|
-
const schema = {
|
|
31
|
-
points: {
|
|
32
|
-
type: [Point]
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const result = await (0, schema_1.clean)(schema, { points: [{ latitude: '1', longitude: 2 }] });
|
|
36
|
-
expect(result).toEqual({ points: [{ latitude: 1, longitude: 2 }] });
|
|
37
|
-
await (0, schema_1.validate)(schema, { points: [{ latitude: 1, longitude: 2 }] });
|
|
38
|
-
});
|
|
39
|
-
it('Should allow passing a typed model to a model', async () => {
|
|
40
|
-
let Point = class Point {
|
|
41
|
-
};
|
|
42
|
-
__decorate([
|
|
43
|
-
(0, _1.Prop)(),
|
|
44
|
-
__metadata("design:type", Number)
|
|
45
|
-
], Point.prototype, "latitude", void 0);
|
|
46
|
-
__decorate([
|
|
47
|
-
(0, _1.Prop)(),
|
|
48
|
-
__metadata("design:type", Number)
|
|
49
|
-
], Point.prototype, "longitude", void 0);
|
|
50
|
-
Point = __decorate([
|
|
51
|
-
(0, _1.TypedModel)()
|
|
52
|
-
], Point);
|
|
53
|
-
const Item = (0, models_1.createModel)({
|
|
54
|
-
name: 'Item',
|
|
55
|
-
schema: {
|
|
56
|
-
points: {
|
|
57
|
-
type: [Point]
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
const result = await Item.clean({ points: [{ latitude: '1', longitude: 2 }] });
|
|
62
|
-
expect.assertions(2);
|
|
63
|
-
expect(result).toEqual({ points: [{ latitude: 1, longitude: 2 }] });
|
|
64
|
-
try {
|
|
65
|
-
await Item.validate({ points: [{ latitude: '1', longitude: 2 }] });
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
expect(error.message).toBe('Validation Error: {points.0.latitude: notANumber}');
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
it('Should allow cleaning and validating just the typed model', async () => {
|
|
72
|
-
let Point = class Point {
|
|
73
|
-
};
|
|
74
|
-
__decorate([
|
|
75
|
-
(0, _1.Prop)(),
|
|
76
|
-
__metadata("design:type", Number)
|
|
77
|
-
], Point.prototype, "latitude", void 0);
|
|
78
|
-
__decorate([
|
|
79
|
-
(0, _1.Prop)(),
|
|
80
|
-
__metadata("design:type", Number)
|
|
81
|
-
], Point.prototype, "longitude", void 0);
|
|
82
|
-
Point = __decorate([
|
|
83
|
-
(0, _1.TypedModel)()
|
|
84
|
-
], Point);
|
|
85
|
-
const result = await (0, schema_1.clean)(Point, { latitude: '1', longitude: 2 });
|
|
86
|
-
expect(result).toEqual({ latitude: 1, longitude: 2 });
|
|
87
|
-
try {
|
|
88
|
-
await (0, schema_1.validate)(Point, { latitude: '1', longitude: 2 });
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
expect(error.message).toBe('Validation Error: {latitude: notANumber}');
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { PropOptions } from '..';
|
|
2
|
-
import { ModelResolversMap } from '@orion-js/models';
|
|
3
|
-
import { ModelResolver, ModelResolverResolve } from '@orion-js/resolvers';
|
|
4
|
-
export declare type PropertiesMap = {
|
|
5
|
-
[key: string]: PropOptions;
|
|
6
|
-
};
|
|
7
|
-
export declare type TypedModelOptions = Record<string, never>;
|
|
8
|
-
export declare class MetadataStorageHandler {
|
|
9
|
-
private schemas;
|
|
10
|
-
private getSchema;
|
|
11
|
-
addSchemaMetadata({ target, options }: {
|
|
12
|
-
target: any;
|
|
13
|
-
options?: TypedModelOptions;
|
|
14
|
-
}): void;
|
|
15
|
-
addPropMetadata({ target, propertyKey, options }: {
|
|
16
|
-
target: any;
|
|
17
|
-
propertyKey: string;
|
|
18
|
-
options: PropOptions;
|
|
19
|
-
}): void;
|
|
20
|
-
addResolverMetadata({ target, propertyKey, options }: {
|
|
21
|
-
target: any;
|
|
22
|
-
propertyKey: string;
|
|
23
|
-
options: ModelResolver<ModelResolverResolve>;
|
|
24
|
-
}): void;
|
|
25
|
-
getSchemaProps(target: any): PropertiesMap | undefined;
|
|
26
|
-
getSchemaResolvers(target: any): ModelResolversMap | undefined;
|
|
27
|
-
}
|
|
28
|
-
export declare const MetadataStorage: MetadataStorageHandler;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MetadataStorage = exports.MetadataStorageHandler = void 0;
|
|
4
|
-
const errors_1 = require("../errors");
|
|
5
|
-
const helpers_1 = require("@orion-js/helpers");
|
|
6
|
-
class MetadataStorageHandler {
|
|
7
|
-
constructor() {
|
|
8
|
-
this.schemas = new Map();
|
|
9
|
-
}
|
|
10
|
-
getSchema(target) {
|
|
11
|
-
const schema = this.schemas.get(target.__schemaId);
|
|
12
|
-
if (schema)
|
|
13
|
-
return schema;
|
|
14
|
-
const schemaId = (0, helpers_1.generateId)();
|
|
15
|
-
target.__schemaId = schemaId;
|
|
16
|
-
const newSchema = {
|
|
17
|
-
schema: target,
|
|
18
|
-
options: {},
|
|
19
|
-
properties: {},
|
|
20
|
-
resolvers: {}
|
|
21
|
-
};
|
|
22
|
-
this.schemas.set(target.__schemaId, newSchema);
|
|
23
|
-
return newSchema;
|
|
24
|
-
}
|
|
25
|
-
addSchemaMetadata({ target, options }) {
|
|
26
|
-
const schema = this.getSchema(target);
|
|
27
|
-
schema.options = options;
|
|
28
|
-
}
|
|
29
|
-
addPropMetadata({ target, propertyKey, options }) {
|
|
30
|
-
const schema = this.getSchema(target);
|
|
31
|
-
const currProp = schema.properties[propertyKey];
|
|
32
|
-
if (currProp) {
|
|
33
|
-
throw new errors_1.PropertyAlreadyExistsError(target.name, propertyKey);
|
|
34
|
-
}
|
|
35
|
-
schema.properties[propertyKey] = options;
|
|
36
|
-
}
|
|
37
|
-
addResolverMetadata({ target, propertyKey, options }) {
|
|
38
|
-
const schema = this.getSchema(target);
|
|
39
|
-
const currResolver = schema.resolvers[propertyKey];
|
|
40
|
-
if (currResolver) {
|
|
41
|
-
throw new errors_1.PropertyAlreadyExistsError(target.name, propertyKey);
|
|
42
|
-
}
|
|
43
|
-
schema.resolvers[propertyKey] = options;
|
|
44
|
-
}
|
|
45
|
-
getSchemaProps(target) {
|
|
46
|
-
const schema = this.getSchema(target);
|
|
47
|
-
return schema.properties;
|
|
48
|
-
}
|
|
49
|
-
getSchemaResolvers(target) {
|
|
50
|
-
const schema = this.getSchema(target);
|
|
51
|
-
return schema.resolvers;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.MetadataStorageHandler = MetadataStorageHandler;
|
|
55
|
-
exports.MetadataStorage = new MetadataStorageHandler();
|
package/lib/utils/interfaces.js
DELETED
package/lib/utils/isClass.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isClass: (type: Function) => boolean;
|
package/lib/utils/isClass.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isClass = void 0;
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
5
|
-
const isClass = (type) => /^class\s/.test(Function.prototype.toString.call(type));
|
|
6
|
-
exports.isClass = isClass;
|