@allgemein/schema-api 1.5.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/LICENSE +23 -0
- package/README.md +34 -0
- package/api/IBaseRef.d.ts +44 -0
- package/api/IBaseRef.js +4 -0
- package/api/IBaseRef.js.map +1 -0
- package/api/IBuildOptions.d.ts +31 -0
- package/api/IBuildOptions.js +4 -0
- package/api/IBuildOptions.js.map +1 -0
- package/api/IClassRef.d.ts +24 -0
- package/api/IClassRef.js +13 -0
- package/api/IClassRef.js.map +1 -0
- package/api/IEntityRef.d.ts +16 -0
- package/api/IEntityRef.js +11 -0
- package/api/IEntityRef.js.map +1 -0
- package/api/IJsonSchema.d.ts +11 -0
- package/api/IJsonSchema.js +26 -0
- package/api/IJsonSchema.js.map +1 -0
- package/api/ILookupRegistry.d.ts +60 -0
- package/api/ILookupRegistry.js +17 -0
- package/api/ILookupRegistry.js.map +1 -0
- package/api/IPropertyExtentions.d.ts +8 -0
- package/api/IPropertyExtentions.js +5 -0
- package/api/IPropertyExtentions.js.map +1 -0
- package/api/IPropertyRef.d.ts +28 -0
- package/api/IPropertyRef.js +13 -0
- package/api/IPropertyRef.js.map +1 -0
- package/api/ISchemaRef.d.ts +8 -0
- package/api/ISchemaRef.js +4 -0
- package/api/ISchemaRef.js.map +1 -0
- package/api/ISerializeSupport.d.ts +11 -0
- package/api/ISerializeSupport.js +26 -0
- package/api/ISerializeSupport.js.map +1 -0
- package/browser.d.ts +62 -0
- package/browser.js +117 -0
- package/browser.js.map +1 -0
- package/decorators/Embeddable.d.ts +2 -0
- package/decorators/Embeddable.js +14 -0
- package/decorators/Embeddable.js.map +1 -0
- package/decorators/Entity.d.ts +2 -0
- package/decorators/Entity.js +14 -0
- package/decorators/Entity.js.map +1 -0
- package/decorators/Namespace.d.ts +1 -0
- package/decorators/Namespace.js +20 -0
- package/decorators/Namespace.js.map +1 -0
- package/decorators/Property.d.ts +3 -0
- package/decorators/Property.js +59 -0
- package/decorators/Property.js.map +1 -0
- package/decorators/PropertyOf.d.ts +2 -0
- package/decorators/PropertyOf.js +28 -0
- package/decorators/PropertyOf.js.map +1 -0
- package/decorators/Schema.d.ts +2 -0
- package/decorators/Schema.js +14 -0
- package/decorators/Schema.js.map +1 -0
- package/decorators/validate/Hostname.d.ts +6 -0
- package/decorators/validate/Hostname.js +38 -0
- package/decorators/validate/Hostname.js.map +1 -0
- package/decorators/validate/Ip4.d.ts +6 -0
- package/decorators/validate/Ip4.js +37 -0
- package/decorators/validate/Ip4.js.map +1 -0
- package/decorators/validate/Ip6.d.ts +5 -0
- package/decorators/validate/Ip6.js +37 -0
- package/decorators/validate/Ip6.js.map +1 -0
- package/decorators/validate/IsEmail.d.ts +5 -0
- package/decorators/validate/IsEmail.js +37 -0
- package/decorators/validate/IsEmail.js.map +1 -0
- package/decorators/validate/IsNotEmpty.d.ts +5 -0
- package/decorators/validate/IsNotEmpty.js +36 -0
- package/decorators/validate/IsNotEmpty.js.map +1 -0
- package/decorators/validate/MaxLength.d.ts +6 -0
- package/decorators/validate/MaxLength.js +34 -0
- package/decorators/validate/MaxLength.js.map +1 -0
- package/decorators/validate/MinLength.d.ts +6 -0
- package/decorators/validate/MinLength.js +34 -0
- package/decorators/validate/MinLength.js.map +1 -0
- package/decorators/validate/Regex.d.ts +7 -0
- package/decorators/validate/Regex.js +46 -0
- package/decorators/validate/Regex.js.map +1 -0
- package/decorators/validate/Required.d.ts +5 -0
- package/decorators/validate/Required.js +34 -0
- package/decorators/validate/Required.js.map +1 -0
- package/decorators/validate/index.d.ts +9 -0
- package/decorators/validate/index.js +21 -0
- package/decorators/validate/index.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +15 -0
- package/index.js.map +1 -0
- package/lib/AbstractRef.d.ts +26 -0
- package/lib/AbstractRef.js +126 -0
- package/lib/AbstractRef.js.map +1 -0
- package/lib/AnnotationsHelper.d.ts +7 -0
- package/lib/AnnotationsHelper.js +97 -0
- package/lib/AnnotationsHelper.js.map +1 -0
- package/lib/ClassRef.d.ts +120 -0
- package/lib/ClassRef.js +319 -0
- package/lib/ClassRef.js.map +1 -0
- package/lib/Constants.d.ts +45 -0
- package/lib/Constants.js +65 -0
- package/lib/Constants.js.map +1 -0
- package/lib/DataContainer.d.ts +31 -0
- package/lib/DataContainer.js +148 -0
- package/lib/DataContainer.js.map +1 -0
- package/lib/LookupRegistry.d.ts +36 -0
- package/lib/LookupRegistry.js +106 -0
- package/lib/LookupRegistry.js.map +1 -0
- package/lib/SchemaRef.d.ts +14 -0
- package/lib/SchemaRef.js +51 -0
- package/lib/SchemaRef.js.map +1 -0
- package/lib/SchemaUtils.d.ts +39 -0
- package/lib/SchemaUtils.js +129 -0
- package/lib/SchemaUtils.js.map +1 -0
- package/lib/json-schema/Constants.d.ts +2 -0
- package/lib/json-schema/Constants.js +7 -0
- package/lib/json-schema/Constants.js.map +1 -0
- package/lib/json-schema/IJsonSchemaSerializeOptions.d.ts +40 -0
- package/lib/json-schema/IJsonSchemaSerializeOptions.js +4 -0
- package/lib/json-schema/IJsonSchemaSerializeOptions.js.map +1 -0
- package/lib/json-schema/IJsonSchemaSerializer.d.ts +7 -0
- package/lib/json-schema/IJsonSchemaSerializer.js +4 -0
- package/lib/json-schema/IJsonSchemaSerializer.js.map +1 -0
- package/lib/json-schema/IJsonSchemaUnserializeOptions.d.ts +41 -0
- package/lib/json-schema/IJsonSchemaUnserializeOptions.js +4 -0
- package/lib/json-schema/IJsonSchemaUnserializeOptions.js.map +1 -0
- package/lib/json-schema/IJsonSchemaUnserializer.d.ts +5 -0
- package/lib/json-schema/IJsonSchemaUnserializer.js +4 -0
- package/lib/json-schema/IJsonSchemaUnserializer.js.map +1 -0
- package/lib/json-schema/IParseOptions.d.ts +16 -0
- package/lib/json-schema/IParseOptions.js +6 -0
- package/lib/json-schema/IParseOptions.js.map +1 -0
- package/lib/json-schema/JsonSchema.d.ts +22 -0
- package/lib/json-schema/JsonSchema.js +109 -0
- package/lib/json-schema/JsonSchema.js.map +1 -0
- package/lib/json-schema/JsonSchema7.d.ts +16 -0
- package/lib/json-schema/JsonSchema7.js +22 -0
- package/lib/json-schema/JsonSchema7.js.map +1 -0
- package/lib/json-schema/JsonSchema7Serializer.d.ts +42 -0
- package/lib/json-schema/JsonSchema7Serializer.js +438 -0
- package/lib/json-schema/JsonSchema7Serializer.js.map +1 -0
- package/lib/json-schema/JsonSchema7Unserializer.d.ts +60 -0
- package/lib/json-schema/JsonSchema7Unserializer.js +499 -0
- package/lib/json-schema/JsonSchema7Unserializer.js.map +1 -0
- package/lib/options/IAbstractOptions.d.ts +19 -0
- package/lib/options/IAbstractOptions.js +4 -0
- package/lib/options/IAbstractOptions.js.map +1 -0
- package/lib/options/IAttributeOptions.d.ts +7 -0
- package/lib/options/IAttributeOptions.js +4 -0
- package/lib/options/IAttributeOptions.js.map +1 -0
- package/lib/options/IEntityOptions.d.ts +4 -0
- package/lib/options/IEntityOptions.js +4 -0
- package/lib/options/IEntityOptions.js.map +1 -0
- package/lib/options/IObjectOptions.d.ts +4 -0
- package/lib/options/IObjectOptions.js +4 -0
- package/lib/options/IObjectOptions.js.map +1 -0
- package/lib/options/IPropertyOptions.d.ts +24 -0
- package/lib/options/IPropertyOptions.js +4 -0
- package/lib/options/IPropertyOptions.js.map +1 -0
- package/lib/options/ISchemaOptions.d.ts +4 -0
- package/lib/options/ISchemaOptions.js +4 -0
- package/lib/options/ISchemaOptions.js.map +1 -0
- package/lib/registry/AbstractRegistry.d.ts +91 -0
- package/lib/registry/AbstractRegistry.js +135 -0
- package/lib/registry/AbstractRegistry.js.map +1 -0
- package/lib/registry/Binding.d.ts +9 -0
- package/lib/registry/Binding.js +17 -0
- package/lib/registry/Binding.js.map +1 -0
- package/lib/registry/DefaultEntityRef.d.ts +30 -0
- package/lib/registry/DefaultEntityRef.js +60 -0
- package/lib/registry/DefaultEntityRef.js.map +1 -0
- package/lib/registry/DefaultNamespacedRegistry.d.ts +153 -0
- package/lib/registry/DefaultNamespacedRegistry.js +511 -0
- package/lib/registry/DefaultNamespacedRegistry.js.map +1 -0
- package/lib/registry/DefaultPropertyRef.d.ts +44 -0
- package/lib/registry/DefaultPropertyRef.js +175 -0
- package/lib/registry/DefaultPropertyRef.js.map +1 -0
- package/lib/registry/MetadataRegistry.d.ts +52 -0
- package/lib/registry/MetadataRegistry.js +186 -0
- package/lib/registry/MetadataRegistry.js.map +1 -0
- package/lib/registry/RegistryFactory.d.ts +43 -0
- package/lib/registry/RegistryFactory.js +109 -0
- package/lib/registry/RegistryFactory.js.map +1 -0
- package/lib/serializer/ISerializeOptions.d.ts +10 -0
- package/lib/serializer/ISerializeOptions.js +4 -0
- package/lib/serializer/ISerializeOptions.js.map +1 -0
- package/lib/serializer/ISerializer.d.ts +5 -0
- package/lib/serializer/ISerializer.js +4 -0
- package/lib/serializer/ISerializer.js.map +1 -0
- package/lib/serializer/ISerializerFactory.d.ts +13 -0
- package/lib/serializer/ISerializerFactory.js +4 -0
- package/lib/serializer/ISerializerFactory.js.map +1 -0
- package/lib/serializer/IUnserializeOptions.d.ts +14 -0
- package/lib/serializer/IUnserializeOptions.js +4 -0
- package/lib/serializer/IUnserializeOptions.js.map +1 -0
- package/lib/serializer/IUnserializer.d.ts +5 -0
- package/lib/serializer/IUnserializer.js +4 -0
- package/lib/serializer/IUnserializer.js.map +1 -0
- package/lib/serializer/SerializerRegistry.d.ts +8 -0
- package/lib/serializer/SerializerRegistry.js +20 -0
- package/lib/serializer/SerializerRegistry.js.map +1 -0
- package/lib/validation/DefaultValidator.d.ts +14 -0
- package/lib/validation/DefaultValidator.js +221 -0
- package/lib/validation/DefaultValidator.js.map +1 -0
- package/lib/validation/IValidateOptions.d.ts +4 -0
- package/lib/validation/IValidateOptions.js +4 -0
- package/lib/validation/IValidateOptions.js.map +1 -0
- package/lib/validation/IValidationError.d.ts +10 -0
- package/lib/validation/IValidationError.js +4 -0
- package/lib/validation/IValidationError.js.map +1 -0
- package/lib/validation/IValidationMessage.d.ts +4 -0
- package/lib/validation/IValidationMessage.js +4 -0
- package/lib/validation/IValidationMessage.js.map +1 -0
- package/lib/validation/IValidationOptions.d.ts +1 -0
- package/lib/validation/IValidationOptions.js +30 -0
- package/lib/validation/IValidationOptions.js.map +1 -0
- package/lib/validation/IValidationResult.d.ts +7 -0
- package/lib/validation/IValidationResult.js +4 -0
- package/lib/validation/IValidationResult.js.map +1 -0
- package/lib/validation/IValidator.d.ts +9 -0
- package/lib/validation/IValidator.js +4 -0
- package/lib/validation/IValidator.js.map +1 -0
- package/lib/validation/IValidatorEntry.d.ts +7 -0
- package/lib/validation/IValidatorEntry.js +4 -0
- package/lib/validation/IValidatorEntry.js.map +1 -0
- package/lib/validation/IValidatorHandle.d.ts +2 -0
- package/lib/validation/IValidatorHandle.js +4 -0
- package/lib/validation/IValidatorHandle.js.map +1 -0
- package/lib/validation/Validator.d.ts +17 -0
- package/lib/validation/Validator.js +54 -0
- package/lib/validation/Validator.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DataContainer = void 0;
|
|
13
|
+
const lodash_1 = require("lodash");
|
|
14
|
+
const Constants_1 = require("./Constants");
|
|
15
|
+
const Validator_1 = require("./validation/Validator");
|
|
16
|
+
const ClassRef_1 = require("./ClassRef");
|
|
17
|
+
const base_1 = require("@allgemein/base");
|
|
18
|
+
/**
|
|
19
|
+
* Container for validation of object
|
|
20
|
+
*/
|
|
21
|
+
class DataContainer {
|
|
22
|
+
constructor(instance, registry) {
|
|
23
|
+
this.errors = [];
|
|
24
|
+
this.validation = {};
|
|
25
|
+
this.instance = instance;
|
|
26
|
+
if (registry) {
|
|
27
|
+
this.ref = lodash_1.isFunction(registry.getEntityRefFor) ?
|
|
28
|
+
registry.getEntityRefFor(instance) : registry;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const clazz = base_1.ClassUtils.getFunction(instance);
|
|
32
|
+
this.ref = ClassRef_1.ClassRef.get(clazz);
|
|
33
|
+
}
|
|
34
|
+
if (!this.ref) {
|
|
35
|
+
throw new Error('none definition found for instance ' + JSON.stringify(instance));
|
|
36
|
+
}
|
|
37
|
+
const properties = this.ref.getPropertyRefs();
|
|
38
|
+
for (const property of properties) {
|
|
39
|
+
this.validation[property.name] = {
|
|
40
|
+
key: property.name,
|
|
41
|
+
valid: false,
|
|
42
|
+
checked: false,
|
|
43
|
+
messages: []
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
;
|
|
47
|
+
}
|
|
48
|
+
addError(e) {
|
|
49
|
+
if (!lodash_1.has(e, 'type')) {
|
|
50
|
+
e.type = 'error';
|
|
51
|
+
}
|
|
52
|
+
this.errors.push(e);
|
|
53
|
+
}
|
|
54
|
+
hasErrors() {
|
|
55
|
+
return this.errors.length > 0;
|
|
56
|
+
}
|
|
57
|
+
checked(str) {
|
|
58
|
+
if (this.validation[str]) {
|
|
59
|
+
return this.validation[str].checked;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
value(str) {
|
|
64
|
+
const wrap = {};
|
|
65
|
+
Object.defineProperty(wrap, str, {
|
|
66
|
+
get: () => {
|
|
67
|
+
return this.instance[str];
|
|
68
|
+
},
|
|
69
|
+
set: (y) => {
|
|
70
|
+
this.instance[str] = y;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return wrap[str];
|
|
74
|
+
}
|
|
75
|
+
valid(str) {
|
|
76
|
+
if (this.validation[str]) {
|
|
77
|
+
return this.validation[str].valid;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
messages(str) {
|
|
82
|
+
if (this.validation[str] && this.validation[str].messages) {
|
|
83
|
+
return this.validation[str].messages;
|
|
84
|
+
}
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
validate() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
this.isValidated = true;
|
|
90
|
+
lodash_1.remove(this.errors, error => error.type === 'validate');
|
|
91
|
+
let results = [];
|
|
92
|
+
try {
|
|
93
|
+
results = (yield Validator_1.Validator.validate(this.instance, this.ref));
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
console.error(e);
|
|
97
|
+
}
|
|
98
|
+
results.map(r => this.errors.push({
|
|
99
|
+
property: r.property,
|
|
100
|
+
value: r.value,
|
|
101
|
+
constraints: r.constraints,
|
|
102
|
+
type: 'validate'
|
|
103
|
+
}));
|
|
104
|
+
this.isSuccessValidated = true;
|
|
105
|
+
lodash_1.keys(this.validation).forEach(key => {
|
|
106
|
+
if (this.validation[key]) {
|
|
107
|
+
const valid = this.validation[key];
|
|
108
|
+
const found = lodash_1.find(this.errors, { property: key });
|
|
109
|
+
valid.messages = [];
|
|
110
|
+
if (found) {
|
|
111
|
+
valid.valid = false;
|
|
112
|
+
Object.keys(found.constraints).forEach(c => {
|
|
113
|
+
valid.messages.push({ type: c, content: found.constraints[c] });
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
valid.valid = true;
|
|
118
|
+
}
|
|
119
|
+
this.isSuccessValidated = this.isSuccessValidated && valid.valid;
|
|
120
|
+
valid.checked = true;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return this.isSuccessValidated;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
applyState() {
|
|
127
|
+
const $state = {};
|
|
128
|
+
DataContainer.keys.forEach(k => {
|
|
129
|
+
const value = lodash_1.get(this, k, null);
|
|
130
|
+
if (lodash_1.isBoolean(value) || !lodash_1.isEmpty(value)) {
|
|
131
|
+
lodash_1.set($state, k, value);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
lodash_1.set(this.instance, Constants_1.STATE_KEY, $state);
|
|
135
|
+
}
|
|
136
|
+
resetErrors() {
|
|
137
|
+
this.errors = [];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.DataContainer = DataContainer;
|
|
141
|
+
DataContainer.keys = [
|
|
142
|
+
'isValidated',
|
|
143
|
+
'isSuccess',
|
|
144
|
+
'isSuccessValidated',
|
|
145
|
+
'errors'
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
//# sourceMappingURL=DataContainer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/DataContainer.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAyF;AAGzF,2CAAsC;AAOtC,sDAAiD;AACjD,yCAAoC;AACpC,0CAA2C;AAE3C;;GAEG;AACH,MAAa,aAAa;IAwBxB,YAAY,QAAW,EAAE,QAAmD;QAT5E,WAAM,GAAuB,EAAE,CAAC;QAEhC,eAAU,GAAuC,EAAE,CAAC;QAQlD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,QAAQ,EAAE;YACZ,IAAI,CAAC,GAAG,GAAG,mBAAU,CAAmB,QAAS,CAAC,eAAe,CAAC,CAAC,CAAC;gBAChD,QAAS,CAAC,eAAe,CAAC,QAAe,CAAC,CAAC,CAAC,CAAC,QAAsB,CAAC;SACzF;aAAM;YACL,MAAM,KAAK,GAAG,iBAAU,CAAC,WAAW,CAAC,QAAe,CAAC,C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YAEjC,IAAI,kBAAS,CAAC,KAAK,CAAC,IAAI,CAAC,gBAAO,CAAC,KAAK,CAAC,EAAE;gBACvC,YAAG,CAAC,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;aACvB;QACH,CAAC,CAAC,CAAC;QAEH,YAAG,CAAM,IAAI,CAAC,QAAQ,EAAE,qBAAS,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC;IAGD,WAAW;QACT,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;;AA7JH,sCA8JC;AA5JQ,kBAAI,GAAa;IACtB,aAAa;IACb,WAAW;IACX,oBAAoB;IACpB,QAAQ;CACT,CAAC","file":"DataContainer.js","sourcesContent":["import {find, get, has, isBoolean, isEmpty, isFunction, keys, remove, set} from 'lodash';\n\n\nimport {STATE_KEY} from './Constants';\nimport {IValidationError} from './validation/IValidationError';\nimport {IValidationResult} from './validation/IValidationResult';\nimport {ILookupRegistry} from '../api/ILookupRegistry';\nimport {IEntityRef} from '../api/IEntityRef';\nimport {IClassRef} from '../api/IClassRef';\nimport {IValidationMessage} from './validation/IValidationMessage';\nimport {Validator} from './validation/Validator';\nimport {ClassRef} from './ClassRef';\nimport {ClassUtils} from '@allgemein/base';\n\n/**\n * Container for validation of object\n */\nexport class DataContainer<T> {\n\n static keys: string[] = [\n 'isValidated',\n 'isSuccess',\n 'isSuccessValidated',\n 'errors'\n ];\n\n isValidated: boolean;\n\n isSuccess: boolean;\n\n isSuccessValidated: boolean;\n\n errors: IValidationError[] = [];\n\n validation: { [k: string]: IValidationResult } = {};\n\n instance: T;\n\n ref: IEntityRef | IClassRef;\n\n\n constructor(instance: T, registry?: ILookupRegistry | IEntityRef | IClassRef) {\n this.instance = instance;\n if (registry) {\n this.ref = isFunction((<ILookupRegistry>registry).getEntityRefFor) ?\n (<ILookupRegistry>registry).getEntityRefFor(instance as any) : registry as IEntityRef;\n } else {\n const clazz = ClassUtils.getFunction(instance as any);\n this.ref = ClassRef.get(clazz) as IClassRef;\n }\n if (!this.ref) {\n throw new Error('none definition found for instance ' + JSON.stringify(instance));\n }\n\n const properties = this.ref.getPropertyRefs();\n for (const property of properties) {\n this.validation[property.name] = {\n key: property.name,\n valid: false,\n checked: false,\n messages: []\n };\n }\n ;\n }\n\n\n addError(e: IValidationError) {\n if (!has(e, 'type')) {\n e.type = 'error';\n }\n this.errors.push(e);\n }\n\n\n hasErrors() {\n return this.errors.length > 0;\n }\n\n\n checked(str: string) {\n if (this.validation[str]) {\n return this.validation[str].checked;\n }\n return false;\n }\n\n\n value(str: string) {\n const wrap = {};\n Object.defineProperty(wrap, str, {\n get: () => {\n return this.instance[str];\n },\n set: (y: any) => {\n this.instance[str] = y;\n }\n });\n return wrap[str];\n }\n\n\n valid(str: string) {\n if (this.validation[str]) {\n return this.validation[str].valid;\n }\n return false;\n }\n\n\n messages(str: string): IValidationMessage[] {\n if (this.validation[str] && this.validation[str].messages) {\n return this.validation[str].messages;\n }\n return [];\n\n }\n\n\n async validate(): Promise<boolean> {\n this.isValidated = true;\n remove(this.errors, error => error.type === 'validate');\n let results: IValidationError[] = [];\n try {\n results = <IValidationError[]>await Validator.validate(this.instance as any, this.ref);\n } catch (e) {\n console.error(e);\n }\n\n results.map(r => this.errors.push({\n property: r.property,\n value: r.value,\n constraints: r.constraints,\n type: 'validate'\n }));\n this.isSuccessValidated = true;\n keys(this.validation).forEach(key => {\n if (this.validation[key]) {\n const valid = this.validation[key];\n const found = find(this.errors, {property: key});\n valid.messages = [];\n if (found) {\n valid.valid = false;\n Object.keys(found.constraints).forEach(c => {\n valid.messages.push({type: c, content: found.constraints[c]});\n });\n } else {\n valid.valid = true;\n }\n this.isSuccessValidated = this.isSuccessValidated && valid.valid;\n valid.checked = true;\n }\n });\n\n return this.isSuccessValidated;\n }\n\n\n applyState() {\n const $state: any = {};\n DataContainer.keys.forEach(k => {\n const value = get(this, k, null);\n\n if (isBoolean(value) || !isEmpty(value)) {\n set($state, k, value);\n }\n });\n\n set(<any>this.instance, STATE_KEY, $state);\n }\n\n\n resetErrors() {\n this.errors = [];\n }\n}\n"],"sourceRoot":".."}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare class LookupRegistry {
|
|
2
|
+
private static $self;
|
|
3
|
+
private namespace;
|
|
4
|
+
private _entries;
|
|
5
|
+
constructor(namespace: string);
|
|
6
|
+
getNamespace(): string;
|
|
7
|
+
static reset(namespace?: string): void;
|
|
8
|
+
static $(namespace?: string): LookupRegistry;
|
|
9
|
+
list(context: string): any[];
|
|
10
|
+
add<T>(context: string, entry: T): T;
|
|
11
|
+
remove<T>(context: string, search: any): T[];
|
|
12
|
+
filter<T>(context: string, search: any): T[];
|
|
13
|
+
find<T>(context: string, search: any): T;
|
|
14
|
+
/**
|
|
15
|
+
* return lookup registry namespaces
|
|
16
|
+
*/
|
|
17
|
+
static getRegistryNamespaces(): string[];
|
|
18
|
+
/**
|
|
19
|
+
* return lookup registries
|
|
20
|
+
*/
|
|
21
|
+
static getLookupRegistries(): LookupRegistry[];
|
|
22
|
+
/**
|
|
23
|
+
* search in all registries
|
|
24
|
+
*
|
|
25
|
+
* @param context
|
|
26
|
+
* @param search
|
|
27
|
+
*/
|
|
28
|
+
static find<T>(context: string, search: any): T;
|
|
29
|
+
/**
|
|
30
|
+
* filter over all registries
|
|
31
|
+
*
|
|
32
|
+
* @param context
|
|
33
|
+
* @param search
|
|
34
|
+
*/
|
|
35
|
+
static filter<T>(context: string, search: any): T[];
|
|
36
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LookupRegistry = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const Constants_1 = require("./Constants");
|
|
6
|
+
class LookupRegistry {
|
|
7
|
+
constructor(namespace) {
|
|
8
|
+
this.namespace = Constants_1.DEFAULT_NAMESPACE;
|
|
9
|
+
this._entries = {};
|
|
10
|
+
this.namespace = namespace;
|
|
11
|
+
}
|
|
12
|
+
getNamespace() {
|
|
13
|
+
return this.namespace;
|
|
14
|
+
}
|
|
15
|
+
static reset(namespace = Constants_1.DEFAULT_NAMESPACE) {
|
|
16
|
+
if (this.$self[namespace]) {
|
|
17
|
+
delete this.$self[namespace];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
static $(namespace = Constants_1.DEFAULT_NAMESPACE) {
|
|
21
|
+
if (!this.$self[namespace]) {
|
|
22
|
+
this.$self[namespace] = new LookupRegistry(namespace);
|
|
23
|
+
}
|
|
24
|
+
return this.$self[namespace];
|
|
25
|
+
}
|
|
26
|
+
list(context) {
|
|
27
|
+
if (!lodash_1.has(this._entries, context)) {
|
|
28
|
+
this._entries[context] = [];
|
|
29
|
+
}
|
|
30
|
+
return this._entries[context];
|
|
31
|
+
}
|
|
32
|
+
add(context, entry) {
|
|
33
|
+
if (!lodash_1.has(this._entries, context)) {
|
|
34
|
+
this._entries[context] = [];
|
|
35
|
+
}
|
|
36
|
+
this._entries[context].push(entry);
|
|
37
|
+
return entry;
|
|
38
|
+
}
|
|
39
|
+
remove(context, search) {
|
|
40
|
+
if (!lodash_1.has(this._entries, context)) {
|
|
41
|
+
this._entries[context] = [];
|
|
42
|
+
}
|
|
43
|
+
return lodash_1.remove(this._entries[context], search);
|
|
44
|
+
}
|
|
45
|
+
filter(context, search) {
|
|
46
|
+
if (!lodash_1.has(this._entries, context)) {
|
|
47
|
+
this._entries[context] = [];
|
|
48
|
+
}
|
|
49
|
+
return lodash_1.filter(this._entries[context], search);
|
|
50
|
+
}
|
|
51
|
+
find(context, search) {
|
|
52
|
+
if (!lodash_1.has(this._entries, context)) {
|
|
53
|
+
this._entries[context] = [];
|
|
54
|
+
}
|
|
55
|
+
return lodash_1.find(this._entries[context], search);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* return lookup registry namespaces
|
|
59
|
+
*/
|
|
60
|
+
static getRegistryNamespaces() {
|
|
61
|
+
return lodash_1.keys(this.$self);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* return lookup registries
|
|
65
|
+
*/
|
|
66
|
+
static getLookupRegistries() {
|
|
67
|
+
return lodash_1.keys(this.$self).map(x => this.$self[x]);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* search in all registries
|
|
71
|
+
*
|
|
72
|
+
* @param context
|
|
73
|
+
* @param search
|
|
74
|
+
*/
|
|
75
|
+
static find(context, search) {
|
|
76
|
+
const registryNames = lodash_1.keys(this.$self);
|
|
77
|
+
for (const registryName of registryNames) {
|
|
78
|
+
const found = this.$self[registryName].find(context, search);
|
|
79
|
+
if (found) {
|
|
80
|
+
return found;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* filter over all registries
|
|
87
|
+
*
|
|
88
|
+
* @param context
|
|
89
|
+
* @param search
|
|
90
|
+
*/
|
|
91
|
+
static filter(context, search) {
|
|
92
|
+
const results = [];
|
|
93
|
+
const namespaces = lodash_1.keys(this.$self);
|
|
94
|
+
for (const namespace of namespaces) {
|
|
95
|
+
const found = this.$self[namespace].filter(context, search);
|
|
96
|
+
if (!lodash_1.isEmpty(found)) {
|
|
97
|
+
results.push(...found);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return results;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.LookupRegistry = LookupRegistry;
|
|
104
|
+
LookupRegistry.$self = {};
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=LookupRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/LookupRegistry.ts"],"names":[],"mappings":";;;AAAA,mCAAgE;AAEhE,2CAA8C;AAE9C,MAAa,cAAc;IAQzB,YAAY,SAAiB;QAJrB,cAAS,GAAW,6BAAiB,CAAC;QAEtC,aAAQ,GAAiC,EAAE,CAAC;QAGlD,IA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AAI,KAAK,EAAE;gBACT,OAAO,KAAK,CAAC;aACd;SACF;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAI,OAAe,EAAE,MAAW;QAC3C,MAAM,OAAO,GAAQ,EAAE,CAAC;QACxB,MAAM,UAAU,GAAG,aAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE;YAClC,MAAM,KAAK,GAAQ,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACjE,IAAI,CAAC,gBAAO,CAAC,KAAK,CAAC,EAAE;gBACnB,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC;aACxB;SACF;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;;AAjHH,wCAoHC;AAlHgB,oBAAK,GAA4C,EAAE,CAAC","file":"LookupRegistry.js","sourcesContent":["import {filter, find, has, isEmpty, keys, remove} from 'lodash';\n\nimport {DEFAULT_NAMESPACE} from './Constants';\n\nexport class LookupRegistry {\n\n private static $self: { [namespace: string]: LookupRegistry } = {};\n\n private namespace: string = DEFAULT_NAMESPACE;\n\n private _entries: { [context: string]: any[] } = {};\n\n constructor(namespace: string) {\n this.namespace = namespace;\n }\n\n getNamespace(): string {\n return this.namespace;\n }\n\n static reset(namespace: string = DEFAULT_NAMESPACE): void {\n if (this.$self[namespace]) {\n delete this.$self[namespace];\n }\n }\n\n static $(namespace: string = DEFAULT_NAMESPACE): LookupRegistry {\n if (!this.$self[namespace]) {\n this.$self[namespace] = new LookupRegistry(namespace);\n }\n return this.$self[namespace];\n }\n\n list(context: string) {\n if (!has(this._entries, context)) {\n this._entries[context] = [];\n }\n return this._entries[context];\n }\n\n add<T>(context: string, entry: T): T {\n if (!has(this._entries, context)) {\n this._entries[context] = [];\n }\n this._entries[context].push(entry);\n return entry;\n }\n\n remove<T>(context: string, search: any): T[] {\n if (!has(this._entries, context)) {\n this._entries[context] = [];\n }\n return remove<T>(this._entries[context], search);\n }\n\n filter<T>(context: string, search: any): T[] {\n if (!has(this._entries, context)) {\n this._entries[context] = [];\n }\n return filter(this._entries[context], search);\n }\n\n find<T>(context: string, search: any): T {\n if (!has(this._entries, context)) {\n this._entries[context] = [];\n }\n return find(this._entries[context], search);\n }\n\n\n /**\n * return lookup registry namespaces\n */\n static getRegistryNamespaces() {\n return keys(this.$self);\n }\n\n /**\n * return lookup registries\n */\n static getLookupRegistries() {\n return keys(this.$self).map(x => this.$self[x]);\n }\n\n /**\n * search in all registries\n *\n * @param context\n * @param search\n */\n static find<T>(context: string, search: any): T {\n const registryNames = keys(this.$self);\n for (const registryName of registryNames) {\n const found = <T>this.$self[registryName].find(context, search);\n if (found) {\n return found;\n }\n }\n return null;\n }\n\n /**\n * filter over all registries\n *\n * @param context\n * @param search\n */\n static filter<T>(context: string, search: any): T[] {\n const results: T[] = [];\n const namespaces = keys(this.$self);\n for (const namespace of namespaces) {\n const found = <T[]>this.$self[namespace].filter(context, search);\n if (!isEmpty(found)) {\n results.push(...found);\n }\n }\n return results;\n }\n\n\n}\n\n\n"],"sourceRoot":".."}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AbstractRef } from './AbstractRef';
|
|
2
|
+
import { ISchemaOptions } from './options/ISchemaOptions';
|
|
3
|
+
import { METADATA_TYPE } from './Constants';
|
|
4
|
+
import { IClassRef } from '../api/IClassRef';
|
|
5
|
+
import { ISchemaRef } from '../api/ISchemaRef';
|
|
6
|
+
import { IEntityRef } from '../api/IEntityRef';
|
|
7
|
+
export declare class SchemaRef extends AbstractRef implements ISchemaRef {
|
|
8
|
+
constructor(options?: ISchemaOptions);
|
|
9
|
+
id(): string;
|
|
10
|
+
getRegistry(): import("..").ILookupRegistry;
|
|
11
|
+
getClassRefFor(object: string | Function | IClassRef, type: METADATA_TYPE): IClassRef;
|
|
12
|
+
getEntityRefFor(value: string | Function): IEntityRef;
|
|
13
|
+
getEntityRefs(): IEntityRef[];
|
|
14
|
+
}
|
package/lib/SchemaRef.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaRef = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const AbstractRef_1 = require("./AbstractRef");
|
|
6
|
+
const Constants_1 = require("./Constants");
|
|
7
|
+
const RegistryFactory_1 = require("./registry/RegistryFactory");
|
|
8
|
+
class SchemaRef extends AbstractRef_1.AbstractRef {
|
|
9
|
+
constructor(options = { name: 'default' }) {
|
|
10
|
+
super(Constants_1.METATYPE_SCHEMA, options.name, null, options.namespace);
|
|
11
|
+
this.setOptions(options);
|
|
12
|
+
}
|
|
13
|
+
id() {
|
|
14
|
+
return this.name.toLowerCase();
|
|
15
|
+
}
|
|
16
|
+
getRegistry() {
|
|
17
|
+
return RegistryFactory_1.RegistryFactory.get(this.namespace);
|
|
18
|
+
}
|
|
19
|
+
getClassRefFor(object, type) {
|
|
20
|
+
return this.getRegistry().getClassRefFor(object, type);
|
|
21
|
+
}
|
|
22
|
+
getEntityRefFor(value) {
|
|
23
|
+
return this.getRegistry()
|
|
24
|
+
.find(Constants_1.METATYPE_ENTITY, (b) => {
|
|
25
|
+
let schemaCheck = false;
|
|
26
|
+
if (lodash_1.isString(value)) {
|
|
27
|
+
schemaCheck = b.name === value || b.getClassRef().name === value;
|
|
28
|
+
}
|
|
29
|
+
else if (lodash_1.isFunction(value)) {
|
|
30
|
+
schemaCheck = b.getClass() === value;
|
|
31
|
+
}
|
|
32
|
+
if (schemaCheck) {
|
|
33
|
+
let schemas = b.getOptions('schema');
|
|
34
|
+
schemas = schemas ? schemas : [];
|
|
35
|
+
return schemas.includes(this.name);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
getEntityRefs() {
|
|
41
|
+
return this.getRegistry()
|
|
42
|
+
.filter(Constants_1.METATYPE_ENTITY, (b) => {
|
|
43
|
+
let schemas = b.getOptions('schema');
|
|
44
|
+
schemas = schemas ? schemas : [];
|
|
45
|
+
return schemas.includes(this.name);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.SchemaRef = SchemaRef;
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=SchemaRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/SchemaRef.ts"],"names":[],"mappings":";;;AAAA,mCAA4C;AAC5C,+CAA0C;AAE1C,2CAA4E;AAC5E,gEAA2D;AAM3D,MAAa,SAAU,SAAQ,yBAAW;IAGxC,YAAY,UAA0B,EAAC,IAAI,EAAE,SAAS,EAAC;QACrD,KAAK,CAAC,2BAAe,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;QAC9D,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAGD,EAAE;QACA,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;IACjC,CAAC;IAGD,WAAW;QACT,OAAO,iCAAe,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC7C,CAAC;IAGD,cAAc,CAAC,MAAqC,EACrC,IAAmB;QAChC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzD,CAAC;IAGD,eAAe,CAAC,KAAwB;QACtC,OAAO,IAAI,CAAC,WAAW,EAAE;aACtB,IAAI,CAAC,2BAAe,EACnB,CAAC,CAAa,EAAE,EAAE;YAChB,IAAI,WAAW,GAAG,KAAK,CAAC;YACxB,IAAI,iBAAQ,CAAC,KAAK,CAAC,EAAE;gBACnB,WAAW,GAAG,CAAC,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC,IAAI,KAAK,KAAK,CAAC;aAClE;iBAAM,IAAI,mBAAU,CAAC,KAAK,CAAC,EAAE;gBAC5B,WAAW,GAAG,CAAC,CAAC,QAAQ,EAAE,KAAK,KAAK,CAAC;aACtC;YACD,IAAI,WAAW,EAAE;gBACf,IAAI,OAAO,GAAG,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;gBACrC,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;gBACjC,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACpC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,CACF,CAAC;IACN,CAAC;IAGD,aAAa;QACX,OAAO,IAAI,CAAC,WAAW,EAAE;aACtB,MAAM,CAAC,2BAAe,EACrB,CAAC,CAAa,EAAE,EAAE;YAChB,IAAI,OAAO,GAAG,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;YACrC,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YACjC,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrC,CAAC,CACF,CAAC;IACN,CAAC;CASF;AAhED,8BAgEC","file":"SchemaRef.js","sourcesContent":["import {isFunction, isString} from 'lodash';\nimport {AbstractRef} from './AbstractRef';\nimport {ISchemaOptions} from './options/ISchemaOptions';\nimport {METADATA_TYPE, METATYPE_ENTITY, METATYPE_SCHEMA} from './Constants';\nimport {RegistryFactory} from './registry/RegistryFactory';\nimport {IClassRef} from '../api/IClassRef';\nimport {ISchemaRef} from '../api/ISchemaRef';\nimport {IEntityRef} from '../api/IEntityRef';\n\n\nexport class SchemaRef extends AbstractRef implements ISchemaRef {\n\n\n constructor(options: ISchemaOptions = {name: 'default'}) {\n super(METATYPE_SCHEMA, options.name, null, options.namespace);\n this.setOptions(options);\n }\n\n\n id() {\n return this.name.toLowerCase();\n }\n\n\n getRegistry() {\n return RegistryFactory.get(this.namespace);\n }\n\n\n getClassRefFor(object: string | Function | IClassRef,\n type: METADATA_TYPE): IClassRef {\n return this.getRegistry().getClassRefFor(object, type);\n }\n\n\n getEntityRefFor(value: string | Function): IEntityRef {\n return this.getRegistry()\n .find(METATYPE_ENTITY,\n (b: IEntityRef) => {\n let schemaCheck = false;\n if (isString(value)) {\n schemaCheck = b.name === value || b.getClassRef().name === value;\n } else if (isFunction(value)) {\n schemaCheck = b.getClass() === value;\n }\n if (schemaCheck) {\n let schemas = b.getOptions('schema');\n schemas = schemas ? schemas : [];\n return schemas.includes(this.name);\n }\n return false;\n }\n );\n }\n\n\n getEntityRefs(): IEntityRef[] {\n return this.getRegistry()\n .filter(METATYPE_ENTITY,\n (b: IEntityRef) => {\n let schemas = b.getOptions('schema');\n schemas = schemas ? schemas : [];\n return schemas.includes(this.name);\n }\n );\n }\n\n\n // getPropertiesFor(fn: Function): IPropertyRef[] {\n // return this.getLookupRegistry()\n // .filter(METATYPE_PROPERTY, (p: IPropertyRef) => p.getSourceRef().getClass() === fn);\n // }\n\n\n}\n\n"],"sourceRoot":".."}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { IClassRef } from '../api/IClassRef';
|
|
2
|
+
import { IEntityRef } from '../api/IEntityRef';
|
|
3
|
+
import { IBuildOptions } from '../api/IBuildOptions';
|
|
4
|
+
export declare class SchemaUtils {
|
|
5
|
+
/**
|
|
6
|
+
* convert an json entry to an instance of given entityRef type
|
|
7
|
+
*
|
|
8
|
+
* - with options can be set pre/post processing for build
|
|
9
|
+
* - beforeBuild - preprocess of an object
|
|
10
|
+
* - afterBuild - postprocess of an object
|
|
11
|
+
* - createAndCopy - if true the only the instance is created, all properties are passed by assign
|
|
12
|
+
*
|
|
13
|
+
* @param entityRef
|
|
14
|
+
* @param data
|
|
15
|
+
* @param options: IBuildOptions
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
static transform<T>(entityRef: IEntityRef | IClassRef, data: any, options?: IBuildOptions): T;
|
|
19
|
+
/**
|
|
20
|
+
* Create a class of given name
|
|
21
|
+
*
|
|
22
|
+
* @param str
|
|
23
|
+
*/
|
|
24
|
+
static clazz(str: string): Function;
|
|
25
|
+
/**
|
|
26
|
+
* Create a class of given name
|
|
27
|
+
*
|
|
28
|
+
* @param str
|
|
29
|
+
*/
|
|
30
|
+
static clazzAnonymous(): Function;
|
|
31
|
+
/**
|
|
32
|
+
* Return inherited class if present else null will be delivered
|
|
33
|
+
*
|
|
34
|
+
* @param klass
|
|
35
|
+
*/
|
|
36
|
+
static getInherited(klass: Function): any;
|
|
37
|
+
static getFunction(klass: any): Function;
|
|
38
|
+
static normValue(value: any): any;
|
|
39
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaUtils = void 0;
|
|
4
|
+
//
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const base_1 = require("@allgemein/base");
|
|
7
|
+
const Constants_1 = require("./Constants");
|
|
8
|
+
const IClassRef_1 = require("../api/IClassRef");
|
|
9
|
+
const IEntityRef_1 = require("../api/IEntityRef");
|
|
10
|
+
let anonId = 0;
|
|
11
|
+
class SchemaUtils {
|
|
12
|
+
/**
|
|
13
|
+
* convert an json entry to an instance of given entityRef type
|
|
14
|
+
*
|
|
15
|
+
* - with options can be set pre/post processing for build
|
|
16
|
+
* - beforeBuild - preprocess of an object
|
|
17
|
+
* - afterBuild - postprocess of an object
|
|
18
|
+
* - createAndCopy - if true the only the instance is created, all properties are passed by assign
|
|
19
|
+
*
|
|
20
|
+
* @param entityRef
|
|
21
|
+
* @param data
|
|
22
|
+
* @param options: IBuildOptions
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
static transform(entityRef, data, options = {}) {
|
|
26
|
+
let object = entityRef.create(!lodash_1.isUndefined(options.skipClassNamespaceInfo) ? !options.skipClassNamespaceInfo : true);
|
|
27
|
+
if (options.beforeBuild) {
|
|
28
|
+
options.beforeBuild(entityRef, data, object, options);
|
|
29
|
+
}
|
|
30
|
+
if (!lodash_1.get(options, Constants_1.OPT_CREAT_AND_COPY, false)) {
|
|
31
|
+
for (let p of entityRef.getPropertyRefs()) {
|
|
32
|
+
if ((lodash_1.isNull(data[p.name]) || lodash_1.isUndefined(data[p.name]))) {
|
|
33
|
+
//object[p.name] = null;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (p.isReference()) {
|
|
37
|
+
let ref = p.getTargetRef();
|
|
38
|
+
if (p.isCollection() || lodash_1.isArray(data[p.name])) {
|
|
39
|
+
object[p.name] = [];
|
|
40
|
+
for (let i = 0; i < data[p.name].length; i++) {
|
|
41
|
+
object[p.name][i] = ref.build(data[p.name][i], options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
object[p.name] = ref.build(data[p.name], options);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
if (p.isCollection() && (lodash_1.isArray(data[p.name]) || lodash_1.isSet(data[p.name]))) {
|
|
50
|
+
object[p.name] = [];
|
|
51
|
+
for (let i = 0; i < data[p.name].length; i++) {
|
|
52
|
+
let v = data[p.name][i];
|
|
53
|
+
if (v) {
|
|
54
|
+
object[p.name][i] = p.convert(v, options);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
object[p.name][i] = null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else if (p.isCollection() && !(lodash_1.isArray(data[p.name]) || lodash_1.isSet(data[p.name]))) {
|
|
62
|
+
throw new base_1.NotYetImplementedError();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
object[p.name] = p.convert(data[p.name], options);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
lodash_1.assign(object, data);
|
|
72
|
+
}
|
|
73
|
+
if (options.afterBuild) {
|
|
74
|
+
options.afterBuild(entityRef, data, object, options);
|
|
75
|
+
}
|
|
76
|
+
return object;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create a class of given name
|
|
80
|
+
*
|
|
81
|
+
* @param str
|
|
82
|
+
*/
|
|
83
|
+
static clazz(str) {
|
|
84
|
+
const X = this.clazzAnonymous();
|
|
85
|
+
Object.defineProperty(X, Constants_1.C_PROP_NAME, { value: str });
|
|
86
|
+
return X;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Create a class of given name
|
|
90
|
+
*
|
|
91
|
+
* @param str
|
|
92
|
+
*/
|
|
93
|
+
static clazzAnonymous() {
|
|
94
|
+
const X = new Function();
|
|
95
|
+
const id = anonId++;
|
|
96
|
+
Object.defineProperty(X, 'anonId', { value: id });
|
|
97
|
+
return X;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Return inherited class if present else null will be delivered
|
|
101
|
+
*
|
|
102
|
+
* @param klass
|
|
103
|
+
*/
|
|
104
|
+
static getInherited(klass) {
|
|
105
|
+
const proto = Reflect.getPrototypeOf(klass);
|
|
106
|
+
if (proto.name && !lodash_1.isEmpty(proto.name) && proto.name !== Object.name && proto.name !== klass.name) {
|
|
107
|
+
return proto;
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
static getFunction(klass) {
|
|
112
|
+
if (IEntityRef_1.isEntityRef(klass)) {
|
|
113
|
+
return klass.getClassRef().getClass(true);
|
|
114
|
+
}
|
|
115
|
+
else if (IClassRef_1.isClassRef(klass)) {
|
|
116
|
+
return klass.getClass(true);
|
|
117
|
+
}
|
|
118
|
+
else if (lodash_1.isFunction(klass)) {
|
|
119
|
+
return klass;
|
|
120
|
+
}
|
|
121
|
+
throw new Error('no class found in ' + klass);
|
|
122
|
+
}
|
|
123
|
+
static normValue(value) {
|
|
124
|
+
return JSON.parse(JSON.stringify(value));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.SchemaUtils = SchemaUtils;
|
|
128
|
+
|
|
129
|
+
//# sourceMappingURL=SchemaUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/SchemaUtils.ts"],"names":[],"mappings":";;;AAAA,EAAE;AACF,mCAA6F;AAC7F,0CAAmE;AACnE,2CAA4D;AAC5D,gDAAuD;AACvD,kDAA0D;AAG1D,IAAI,MAAM,GAAG,CAAC,CAAC;AAEf,MAAa,WAAW;IAEtB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,SAAS,CAAI,SAAiC,EAAE,IAAS,EAAE,UAAyB,EAAE;QAC3F,IAAI,MAAM,GAAM,SAAS,CAAC,MAAM,CAAC,CAAC,oBAAW,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACxH,IAAI,OAAO,CAAC,WAAW,EAAE;YACvB,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;SACvD;QAED,IAAI,CAAC,YAAG,CAAC,OAAO,EAAE,8BAAkB,EAAE,KAAK,CAAC,EAAE;YAE5C,KAAK,IAAI,CAAC,IAAI,SAAS,CAAC,eAAe,EAAE,EAAE;gBACzC,IAAI,CAAC,eAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,oBAAW,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;oBACvD,wBAAwB;oBACxB,SAAS;iBACV;gBACD,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE;oBACnB,IAAI,GAAG,GAAG,CAAC,CAAC,YAAY,EAAE,CAAC;oBAC3B,IA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;IACH,MAAM,CAAC,YAAY,CAAC,KAAe;QACjC,MAAM,KAAK,GAAG,OAAO,CAAC,cAAc,CAAC,KAAK,CAAQ,CAAC;QACnD,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,gBAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,EAAE;YACjG,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAGD,MAAM,CAAC,WAAW,CAAC,KAAU;QAC3B,IAAI,wBAAW,CAAC,KAAK,CAAC,EAAE;YACtB,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;SAC3C;aAAM,IAAI,sBAAU,CAAC,KAAK,CAAC,EAAE;YAC5B,OAAO,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;SAC7B;aAAM,IAAI,mBAAU,CAAC,KAAK,CAAC,EAAE;YAC5B,OAAO,KAAK,CAAC;SACd;QACD,MAAM,IAAI,KAAK,CAAC,oBAAoB,GAAG,KAAK,CAAC,CAAC;IAChD,CAAC;IAGD,MAAM,CAAC,SAAS,CAAC,KAAU;QACzB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC;CAYF;AAlID,kCAkIC","file":"SchemaUtils.js","sourcesContent":["//\nimport {assign, get, isArray, isEmpty, isFunction, isNull, isSet, isUndefined} from 'lodash';\nimport {CryptUtils, NotYetImplementedError} from '@allgemein/base';\nimport {C_PROP_NAME, OPT_CREAT_AND_COPY} from './Constants';\nimport {IClassRef, isClassRef} from '../api/IClassRef';\nimport {IEntityRef, isEntityRef} from '../api/IEntityRef';\nimport {IBuildOptions} from '../api/IBuildOptions';\n\nlet anonId = 0;\n\nexport class SchemaUtils {\n\n /**\n * convert an json entry to an instance of given entityRef type\n *\n * - with options can be set pre/post processing for build\n * - beforeBuild - preprocess of an object\n * - afterBuild - postprocess of an object\n * - createAndCopy - if true the only the instance is created, all properties are passed by assign\n *\n * @param entityRef\n * @param data\n * @param options: IBuildOptions\n *\n */\n static transform<T>(entityRef: IEntityRef | IClassRef, data: any, options: IBuildOptions = {}): T {\n let object: T = entityRef.create(!isUndefined(options.skipClassNamespaceInfo) ? !options.skipClassNamespaceInfo : true);\n if (options.beforeBuild) {\n options.beforeBuild(entityRef, data, object, options);\n }\n\n if (!get(options, OPT_CREAT_AND_COPY, false)) {\n\n for (let p of entityRef.getPropertyRefs()) {\n if ((isNull(data[p.name]) || isUndefined(data[p.name]))) {\n //object[p.name] = null;\n continue;\n }\n if (p.isReference()) {\n let ref = p.getTargetRef();\n if (p.isCollection() || isArray(data[p.name])) {\n object[p.name] = [];\n for (let i = 0; i < data[p.name].length; i++) {\n object[p.name][i] = ref.build(data[p.name][i], options);\n }\n } else {\n object[p.name] = ref.build(data[p.name], options);\n }\n } else {\n if (p.isCollection() && (isArray(data[p.name]) || isSet(data[p.name]))) {\n object[p.name] = [];\n for (let i = 0; i < data[p.name].length; i++) {\n let v = data[p.name][i];\n if (v) {\n object[p.name][i] = p.convert(v, options);\n } else {\n object[p.name][i] = null;\n }\n }\n } else if (p.isCollection() && !(isArray(data[p.name]) || isSet(data[p.name]))) {\n throw new NotYetImplementedError();\n } else {\n object[p.name] = p.convert(data[p.name], options);\n }\n }\n }\n } else {\n assign(object, data);\n }\n\n if (options.afterBuild) {\n options.afterBuild(entityRef, data, object, options);\n }\n return object;\n\n }\n\n /**\n * Create a class of given name\n *\n * @param str\n */\n static clazz(str: string): Function {\n const X = this.clazzAnonymous();\n Object.defineProperty(X, C_PROP_NAME, {value: str});\n return X;\n }\n\n /**\n * Create a class of given name\n *\n * @param str\n */\n static clazzAnonymous(): Function {\n const X = new Function();\n const id = anonId++;\n Object.defineProperty(X, 'anonId', {value: id});\n return X;\n }\n\n /**\n * Return inherited class if present else null will be delivered\n *\n * @param klass\n */\n static getInherited(klass: Function) {\n const proto = Reflect.getPrototypeOf(klass) as any;\n if (proto.name && !isEmpty(proto.name) && proto.name !== Object.name && proto.name !== klass.name) {\n return proto;\n }\n return null;\n }\n\n\n static getFunction(klass: any) {\n if (isEntityRef(klass)) {\n return klass.getClassRef().getClass(true);\n } else if (isClassRef(klass)) {\n return klass.getClass(true);\n } else if (isFunction(klass)) {\n return klass;\n }\n throw new Error('no class found in ' + klass);\n }\n\n\n static normValue(value: any) {\n return JSON.parse(JSON.stringify(value));\n }\n\n //\n //\n // static interprete(value: string) {\n // // json\n // // date\n // // number\n // //\n // }\n //\n\n}\n"],"sourceRoot":".."}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/json-schema/Constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,sBAAsB,GAAG,aAAa,CAAC;AACvC,QAAA,QAAQ,GAAG,UAAU,CAAC","file":"Constants.js","sourcesContent":["export const JSON_SCHEMA_SERIALIZER = 'json-schema';\nexport const DRAFT_07 = 'draft-07';\n"],"sourceRoot":"../.."}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IJsonSchemaSerializer } from './IJsonSchemaSerializer';
|
|
2
|
+
import { ISerializeOptions } from '../serializer/ISerializeOptions';
|
|
3
|
+
export interface IJsonSchemaSerializeOptions extends ISerializeOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Version of json schema
|
|
6
|
+
*/
|
|
7
|
+
version?: 'draft-07' | string;
|
|
8
|
+
/**
|
|
9
|
+
* If multiple schemas
|
|
10
|
+
*/
|
|
11
|
+
handleMultipleSchemas?: 'reference' | 'clone';
|
|
12
|
+
/**
|
|
13
|
+
* If set then on entries with type object the class function will be passed under key $target
|
|
14
|
+
*
|
|
15
|
+
* {
|
|
16
|
+
* title: 'TestClass',
|
|
17
|
+
* type: 'object',
|
|
18
|
+
* $target: ClassType<TestClass>
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
appendTarget?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Callback for postprocess
|
|
24
|
+
*/
|
|
25
|
+
postProcess?: (src: any, dst: any, serializer?: IJsonSchemaSerializer) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Allow override of existing standard properties
|
|
28
|
+
*/
|
|
29
|
+
allowKeyOverride?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Property keys where information can be skipped
|
|
32
|
+
* Default if not set: ['type', '$ref', 'target', '$target','propertyName', 'metaType', 'namespace', 'name']
|
|
33
|
+
* See constant DEFAULT_KEY_TO_SKIP
|
|
34
|
+
*/
|
|
35
|
+
keysToSkip?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* By default options of reference properties are removed, to pass them set this to 'false'.
|
|
38
|
+
*/
|
|
39
|
+
deleteReferenceKeys?: boolean;
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/json-schema/IJsonSchemaSerializeOptions.ts"],"names":[],"mappings":"","file":"IJsonSchemaSerializeOptions.js","sourcesContent":["import {IJsonSchemaSerializer} from './IJsonSchemaSerializer';\nimport {ISerializeOptions} from '../serializer/ISerializeOptions';\n\nexport interface IJsonSchemaSerializeOptions extends ISerializeOptions {\n\n /**\n * Version of json schema\n */\n version?: 'draft-07' | string;\n\n /**\n * If multiple schemas\n */\n handleMultipleSchemas?: 'reference' | 'clone';\n\n\n /**\n * If set then on entries with type object the class function will be passed under key $target\n *\n * {\n * title: 'TestClass',\n * type: 'object',\n * $target: ClassType<TestClass>\n * }\n */\n appendTarget?: boolean;\n\n\n /**\n * Callback for postprocess\n */\n postProcess?: (src: any, dst: any, serializer?: IJsonSchemaSerializer) => void;\n\n\n /**\n * Allow override of existing standard properties\n */\n allowKeyOverride?: boolean;\n\n /**\n * Property keys where information can be skipped\n * Default if not set: ['type', '$ref', 'target', '$target','propertyName', 'metaType', 'namespace', 'name']\n * See constant DEFAULT_KEY_TO_SKIP\n */\n keysToSkip?: string[];\n\n /**\n * By default options of reference properties are removed, to pass them set this to 'false'.\n */\n deleteReferenceKeys?: boolean;\n}\n"],"sourceRoot":"../.."}
|