@vsaas/loopback-datasource-juggler 13.0.2 → 13.1.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.js +2 -0
- package/dist/lib/dao.js +16 -5
- package/dist/lib/model-builder.js +14 -1
- package/dist/lib/model-utils.js +12 -1
- package/dist/lib/normalizers.js +191 -0
- package/dist/lib/property-constraints.js +167 -0
- package/dist/lib/validations.js +34 -0
- package/dist/package.js +1 -1
- package/package.json +2 -2
- package/types/model.d.ts +64 -0
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const require_lib_globalize = require("./lib/globalize.js");
|
|
3
3
|
const require_lib_geo = require("./lib/geo.js");
|
|
4
|
+
const require_lib_normalizers = require("./lib/normalizers.js");
|
|
4
5
|
const require_lib_validations = require("./lib/validations.js");
|
|
5
6
|
const require_lib_model = require("./lib/model.js");
|
|
6
7
|
const require_lib_model_builder = require("./lib/model-builder.js");
|
|
@@ -14,6 +15,7 @@ exports.DataSource = exports.Schema = require_lib_datasource.DataSource;
|
|
|
14
15
|
exports.ModelBaseClass = require_lib_model;
|
|
15
16
|
exports.GeoPoint = require_lib_geo.GeoPoint;
|
|
16
17
|
exports.ValidationError = require_lib_validations.ValidationError;
|
|
18
|
+
exports.registerNormalizer = require_lib_normalizers.registerNormalizer;
|
|
17
19
|
Object.defineProperty(exports, "version", { get: function() {
|
|
18
20
|
return require_package$1.default.version;
|
|
19
21
|
} });
|
package/dist/lib/dao.js
CHANGED
|
@@ -4,6 +4,7 @@ const require_lib_jutil = require("./jutil.js");
|
|
|
4
4
|
const require_lib_geo = require("./geo.js");
|
|
5
5
|
const require_lib_utils = require("./utils.js");
|
|
6
6
|
const require_lib_list = require("./list.js");
|
|
7
|
+
const require_lib_normalizers = require("./normalizers.js");
|
|
7
8
|
require("./model-utils.js");
|
|
8
9
|
const require_lib_validations = require("./validations.js");
|
|
9
10
|
require("./model.js");
|
|
@@ -23,13 +24,15 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
23
24
|
*/
|
|
24
25
|
const g = require_lib_globalize();
|
|
25
26
|
const jutil = require_lib_jutil;
|
|
26
|
-
const ValidationError = require_lib_validations
|
|
27
|
+
const { ValidationError, checkPropertyConstraints } = require_lib_validations;
|
|
28
|
+
const CONSTRAINTS_ONLY = Symbol("constraintsOnly");
|
|
27
29
|
const Relation = require_lib_relations;
|
|
28
30
|
const Inclusion = require_lib_include;
|
|
29
31
|
const List = require_lib_list;
|
|
30
32
|
const geo = require_lib_geo;
|
|
31
33
|
const Memory = require_lib_connectors_memory.Memory;
|
|
32
34
|
const utils = require_lib_utils;
|
|
35
|
+
const { normalizeData } = require_lib_normalizers;
|
|
33
36
|
utils.fieldsToArray;
|
|
34
37
|
utils.sanitizeQuery;
|
|
35
38
|
const setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
|
@@ -613,6 +616,7 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
613
616
|
if (this.settings.forceId) {
|
|
614
617
|
options = Object.create(options);
|
|
615
618
|
options.validate = !!doValidate;
|
|
619
|
+
if (doValidate === void 0) options[CONSTRAINTS_ONLY] = true;
|
|
616
620
|
Model.findById(id, withoutLeanOption(options), function(err, model) {
|
|
617
621
|
if (err) return cb(err);
|
|
618
622
|
if (!model) return cb(errorModelNotFound(id));
|
|
@@ -652,8 +656,10 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
652
656
|
if (!valid) {
|
|
653
657
|
if (doValidate) return cb(new ValidationError(inst), inst);
|
|
654
658
|
else {
|
|
659
|
+
const ignored = new ValidationError(inst).message;
|
|
660
|
+
if (!checkPropertyConstraints(inst, Object.keys(inst.__data))) return cb(new ValidationError(inst), inst);
|
|
655
661
|
g.warn("Ignoring validation errors in {{updateOrCreate()}}:");
|
|
656
|
-
g.warn(" %s",
|
|
662
|
+
g.warn(" %s", ignored);
|
|
657
663
|
}
|
|
658
664
|
}
|
|
659
665
|
callConnector();
|
|
@@ -1822,7 +1828,7 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1822
1828
|
};
|
|
1823
1829
|
Model.notifyObserversOf("before save", context, function(err) {
|
|
1824
1830
|
if (err) return cb(err);
|
|
1825
|
-
let data = inst.toObject(true);
|
|
1831
|
+
let data = normalizeData(Model, inst.toObject(true));
|
|
1826
1832
|
Model.applyProperties(data, inst);
|
|
1827
1833
|
inst.setAttributes(data);
|
|
1828
1834
|
if (!options.validate) return save();
|
|
@@ -1937,9 +1943,11 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1937
1943
|
this.applyScope(query);
|
|
1938
1944
|
this.applyProperties(data);
|
|
1939
1945
|
let doValidate = false;
|
|
1946
|
+
let checkConstraints = false;
|
|
1940
1947
|
if (options.validate === void 0) {
|
|
1941
1948
|
if (Model.settings.validateUpdate === void 0) {
|
|
1942
1949
|
if (Model.settings.automaticValidation !== void 0) doValidate = Model.settings.automaticValidation;
|
|
1950
|
+
else checkConstraints = true;
|
|
1943
1951
|
} else doValidate = Model.settings.validateUpdate;
|
|
1944
1952
|
} else doValidate = options.validate;
|
|
1945
1953
|
where = query.where;
|
|
@@ -1967,8 +1975,10 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1967
1975
|
} catch (err) {
|
|
1968
1976
|
return cb(err);
|
|
1969
1977
|
}
|
|
1970
|
-
if (doValidate === false)
|
|
1971
|
-
|
|
1978
|
+
if (doValidate === false) {
|
|
1979
|
+
if (checkConstraints && !checkPropertyConstraints(inst, Object.keys(inst.__data))) return cb(new ValidationError(inst));
|
|
1980
|
+
doUpdate(ctx.where, ctx.data);
|
|
1981
|
+
} else inst.isValid(function(valid) {
|
|
1972
1982
|
if (!valid) return cb(new ValidationError(inst));
|
|
1973
1983
|
doUpdate(ctx.where, ctx.data);
|
|
1974
1984
|
}, options);
|
|
@@ -2395,6 +2405,7 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
2395
2405
|
}
|
|
2396
2406
|
triggerSave();
|
|
2397
2407
|
}, data, options);
|
|
2408
|
+
else if (options[CONSTRAINTS_ONLY] && !checkPropertyConstraints(inst, Object.keys(data || {}))) cb(new ValidationError(inst), inst);
|
|
2398
2409
|
else triggerSave();
|
|
2399
2410
|
function triggerSave() {
|
|
2400
2411
|
inst.trigger("save", function(saveDone) {
|
|
@@ -4,10 +4,13 @@ const require_lib_globalize = require("./globalize.js");
|
|
|
4
4
|
const require_lib_types = require("./types.js");
|
|
5
5
|
const require_lib_utils = require("./utils.js");
|
|
6
6
|
const require_lib_list = require("./list.js");
|
|
7
|
+
const require_lib_normalizers = require("./normalizers.js");
|
|
8
|
+
const require_lib_validations = require("./validations.js");
|
|
7
9
|
const require_lib_model_internals = require("./model-internals.js");
|
|
8
10
|
const require_lib_model = require("./model.js");
|
|
9
11
|
const require_lib_model_definition = require("./model-definition.js");
|
|
10
12
|
const require_lib_mixins = require("./mixins.js");
|
|
13
|
+
const require_lib_property_constraints = require("./property-constraints.js");
|
|
11
14
|
const require_lib_introspection = require("./introspection.js");
|
|
12
15
|
//#region src/lib/model-builder.ts
|
|
13
16
|
var require_model_builder = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) => {
|
|
@@ -25,6 +28,9 @@ var require_model_builder = /* @__PURE__ */ require_runtime.__commonJSMin(((expo
|
|
|
25
28
|
const ModelDefinition = require_lib_model_definition;
|
|
26
29
|
const MixinProvider = require_lib_mixins;
|
|
27
30
|
const { getNestedModelOptions, skipsCustomSetters } = require_lib_model_internals;
|
|
31
|
+
const { getNormalizer, normalizeValue } = require_lib_normalizers;
|
|
32
|
+
const { getPropertyConstraints } = require_lib_property_constraints;
|
|
33
|
+
const { addPropertyConstraints } = require_lib_validations;
|
|
28
34
|
const { deepMerge, deepMergeProperty, rankArrayElements, isClass, applyParentProperty } = require_lib_utils;
|
|
29
35
|
require_lib_types(ModelBuilder);
|
|
30
36
|
const introspect = require_lib_introspection(ModelBuilder);
|
|
@@ -322,6 +328,10 @@ var require_model_builder = /* @__PURE__ */ require_runtime.__commonJSMin(((expo
|
|
|
322
328
|
ModelClass.validatesPresenceOf(propertyName, requiredOptions);
|
|
323
329
|
}
|
|
324
330
|
if (DataType === Date) ModelClass.validatesDateOf(propertyName);
|
|
331
|
+
const label = ModelClass.modelName + "." + propertyName;
|
|
332
|
+
getNormalizer(prop, label);
|
|
333
|
+
const constraints = getPropertyConstraints(prop, label);
|
|
334
|
+
if (constraints.length) addPropertyConstraints(ModelClass, propertyName, constraints);
|
|
325
335
|
let classCheckType;
|
|
326
336
|
let classCheckResult;
|
|
327
337
|
const isClassType = function(type) {
|
|
@@ -347,7 +357,8 @@ var require_model_builder = /* @__PURE__ */ require_runtime.__commonJSMin(((expo
|
|
|
347
357
|
else return this.__data && this.__data[propertyName];
|
|
348
358
|
},
|
|
349
359
|
set: function(value) {
|
|
350
|
-
|
|
360
|
+
const property = ModelClass.definition.properties[propertyName];
|
|
361
|
+
let DataType = property.type;
|
|
351
362
|
if (Array.isArray(DataType) || DataType === Array) DataType = List;
|
|
352
363
|
else if (DataType === Date) DataType = DateType;
|
|
353
364
|
else if (DataType === Boolean) DataType = BooleanType;
|
|
@@ -367,6 +378,8 @@ var require_model_builder = /* @__PURE__ */ require_runtime.__commonJSMin(((expo
|
|
|
367
378
|
this.__data[propertyName] = value instanceof DataType ? value : modelOptions ? isClassType(DataType) ? new DataType(value, modelOptions) : DataType(value, modelOptions) : isClassType(DataType) ? new DataType(value) : DataType(value);
|
|
368
379
|
if (value && this.__data[propertyName] instanceof DefaultModelBaseClass) applyParentProperty(this.__data[propertyName], this);
|
|
369
380
|
}
|
|
381
|
+
const normalize = getNormalizer(property);
|
|
382
|
+
if (normalize !== void 0 && !skipsCustomSetters(this)) this.__data[propertyName] = normalizeValue(this.__data[propertyName], normalize);
|
|
370
383
|
}
|
|
371
384
|
}
|
|
372
385
|
},
|
package/dist/lib/model-utils.js
CHANGED
|
@@ -2,6 +2,7 @@ const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
|
2
2
|
const require_lib_globalize = require("./globalize.js");
|
|
3
3
|
const require_lib_geo = require("./geo.js");
|
|
4
4
|
const require_lib_utils = require("./utils.js");
|
|
5
|
+
const require_lib_normalizers = require("./normalizers.js");
|
|
5
6
|
require("./model.js");
|
|
6
7
|
//#region src/lib/model-utils.ts
|
|
7
8
|
var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
@@ -12,6 +13,14 @@ var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
12
13
|
const g = require_lib_globalize();
|
|
13
14
|
const geo = require_lib_geo;
|
|
14
15
|
const { fieldsToArray, sanitizeQuery: sanitizeQueryOrData, isPlainObject, isClass, toRegExp } = require_lib_utils;
|
|
16
|
+
const { getNormalizer, normalizeValue } = require_lib_normalizers;
|
|
17
|
+
const NORMALIZED_OPERATORS = /* @__PURE__ */ new Set([
|
|
18
|
+
null,
|
|
19
|
+
"eq",
|
|
20
|
+
"neq",
|
|
21
|
+
"inq",
|
|
22
|
+
"nin"
|
|
23
|
+
]);
|
|
15
24
|
/**
|
|
16
25
|
* A mixin to contain utility methods for DataAccessObject
|
|
17
26
|
*/
|
|
@@ -250,7 +259,7 @@ var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
250
259
|
err.statusCode = 400;
|
|
251
260
|
throw err;
|
|
252
261
|
}
|
|
253
|
-
for (let k = 0; k < clauses.length; k++) self._coerce(clauses[k], options);
|
|
262
|
+
for (let k = 0; k < clauses.length; k++) self._coerce(clauses[k], options, modelDef);
|
|
254
263
|
where[p] = clauses;
|
|
255
264
|
continue;
|
|
256
265
|
}
|
|
@@ -343,6 +352,8 @@ var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
343
352
|
} else val = isClass(DataType) ? new DataType(val) : DataType(val);
|
|
344
353
|
}
|
|
345
354
|
}
|
|
355
|
+
const normalize = getNormalizer(props[p]);
|
|
356
|
+
if (normalize !== void 0 && NORMALIZED_OPERATORS.has(operator)) val = normalizeValue(val, normalize);
|
|
346
357
|
if (operator && operator !== "eq") {
|
|
347
358
|
const value = {};
|
|
348
359
|
value[operator] = val;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_globalize = require("./globalize.js");
|
|
4
|
+
//#region src/lib/normalizers.ts
|
|
5
|
+
var require_normalizers = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
/*!
|
|
7
|
+
* Property normalizers rewrite string values before validation and persistence.
|
|
8
|
+
*
|
|
9
|
+
* A property opts in with `normalize: 'trim'` or an ordered list such as
|
|
10
|
+
* `normalize: ['trim', 'lowercase']`. Entries are names from the registry below
|
|
11
|
+
* or inline functions. Each step receives a string and returns a string, or
|
|
12
|
+
* null/undefined to stop the pipeline and store that value instead.
|
|
13
|
+
*/
|
|
14
|
+
const g = require_lib_globalize();
|
|
15
|
+
const INVISIBLE_CHARS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F\u00AD\u200B\u202A-\u202E\u2060\u2066-\u2069\uFEFF]/g;
|
|
16
|
+
const COMBINING_MARKS = /\p{Mn}/gu;
|
|
17
|
+
const NON_DIGITS = /\P{Nd}/gu;
|
|
18
|
+
const NON_ALPHANUMERIC = /[^\p{L}\p{Nd}]/gu;
|
|
19
|
+
const WHITESPACE_RUNS = /\s+/g;
|
|
20
|
+
const TAG_START = /[A-Za-z/!?]/;
|
|
21
|
+
const RAW_TEXT_TAG = /^<(script|style)[\s/>]/i;
|
|
22
|
+
const SCRIPT_CLOSE = /<\/script[^>]*(?:>|$)/gi;
|
|
23
|
+
const STYLE_CLOSE = /<\/style[^>]*(?:>|$)/gi;
|
|
24
|
+
const registry = Object.create(null);
|
|
25
|
+
registry.trim = (s) => s.trim();
|
|
26
|
+
registry.lowercase = (s) => s.toLowerCase();
|
|
27
|
+
registry.uppercase = (s) => s.toUpperCase();
|
|
28
|
+
registry.nfc = (s) => s.normalize("NFC");
|
|
29
|
+
registry.unaccent = (s) => s.normalize("NFD").replace(COMBINING_MARKS, "").normalize("NFC");
|
|
30
|
+
registry.collapseSpaces = (s) => s.replace(WHITESPACE_RUNS, " ");
|
|
31
|
+
registry.emptyToNull = (s) => s === "" ? null : s;
|
|
32
|
+
registry.stripInvisible = (s) => s.replace(INVISIBLE_CHARS, "");
|
|
33
|
+
registry.numeric = (s) => s.replace(NON_DIGITS, "");
|
|
34
|
+
registry.alphanumeric = (s) => s.replace(NON_ALPHANUMERIC, "");
|
|
35
|
+
registry.stripHtml = stripHtml;
|
|
36
|
+
/**
|
|
37
|
+
* Remove markup and keep the text, without decoding or escaping entities.
|
|
38
|
+
* Comments, doctypes, processing instructions and tags are dropped; script and
|
|
39
|
+
* style elements are dropped with their content. A `<` that does not start
|
|
40
|
+
* markup is kept as text, except when removing a tag would join it to the next
|
|
41
|
+
* character, so the output never contains markup and a second pass is a no-op.
|
|
42
|
+
* Runs in linear time.
|
|
43
|
+
*/
|
|
44
|
+
function stripHtml(s) {
|
|
45
|
+
if (s.indexOf("<") === -1) return s;
|
|
46
|
+
let hasGt = true;
|
|
47
|
+
let hasCommentEnd = true;
|
|
48
|
+
function markupEnd(lt) {
|
|
49
|
+
if (hasCommentEnd && s.startsWith("<!--", lt)) {
|
|
50
|
+
const close = s.indexOf("-->", lt + 2);
|
|
51
|
+
if (close !== -1) return close + 3;
|
|
52
|
+
hasCommentEnd = false;
|
|
53
|
+
}
|
|
54
|
+
const gt = hasGt ? s.indexOf(">", lt + 1) : -1;
|
|
55
|
+
if (gt === -1) {
|
|
56
|
+
hasGt = false;
|
|
57
|
+
return -1;
|
|
58
|
+
}
|
|
59
|
+
const raw = RAW_TEXT_TAG.exec(s.slice(lt, Math.min(gt + 1, lt + 8)));
|
|
60
|
+
if (raw) {
|
|
61
|
+
const closeTag = raw[1].toLowerCase() === "script" ? SCRIPT_CLOSE : STYLE_CLOSE;
|
|
62
|
+
closeTag.lastIndex = gt + 1;
|
|
63
|
+
const close = closeTag.exec(s);
|
|
64
|
+
return close ? close.index + close[0].length : s.length;
|
|
65
|
+
}
|
|
66
|
+
return gt + 1;
|
|
67
|
+
}
|
|
68
|
+
let out = "";
|
|
69
|
+
let i = 0;
|
|
70
|
+
while (i < s.length) {
|
|
71
|
+
const lt = s.indexOf("<", i);
|
|
72
|
+
if (lt === -1) {
|
|
73
|
+
out += s.slice(i);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
out += s.slice(i, lt);
|
|
77
|
+
const end = TAG_START.test(s.charAt(lt + 1)) ? markupEnd(lt) : -1;
|
|
78
|
+
if (end === -1) {
|
|
79
|
+
out += "<";
|
|
80
|
+
i = lt + 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
let keep = out.length;
|
|
84
|
+
while (keep > 0 && out.charCodeAt(keep - 1) === 60) keep--;
|
|
85
|
+
out = out.slice(0, keep);
|
|
86
|
+
i = end;
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Register a named normalizer usable from property definitions.
|
|
92
|
+
* Register before defining the models that reference it.
|
|
93
|
+
* @param {String} name Normalizer name
|
|
94
|
+
* @param {Function} fn `(value: string) => string | null | undefined`
|
|
95
|
+
*/
|
|
96
|
+
function registerNormalizer(name, fn) {
|
|
97
|
+
if (typeof name !== "string" || !name) throw new TypeError(g.f("Normalizer name must be a non-empty string"));
|
|
98
|
+
if (typeof fn !== "function") throw new TypeError(g.f("Normalizer \"%s\" must be a function", name));
|
|
99
|
+
if (registry[name]) throw new Error(g.f("Normalizer \"%s\" is already registered", name));
|
|
100
|
+
registry[name] = fn;
|
|
101
|
+
}
|
|
102
|
+
const compiled = /* @__PURE__ */ new WeakMap();
|
|
103
|
+
/**
|
|
104
|
+
* Get the compiled normalizer of a built property definition.
|
|
105
|
+
* Throws when the definition references an unknown normalizer or is applied to
|
|
106
|
+
* a non-string property.
|
|
107
|
+
* @param {Object} property Built property definition (with resolved `type`)
|
|
108
|
+
* @param {String} [label] `Model.property`, used in error messages
|
|
109
|
+
* @returns {Function|undefined}
|
|
110
|
+
*/
|
|
111
|
+
function getNormalizer(property, label) {
|
|
112
|
+
if (property == null || property.normalize == null || property.normalize === false) return;
|
|
113
|
+
let fn = compiled.get(property);
|
|
114
|
+
if (fn === void 0) {
|
|
115
|
+
fn = compile(property, label);
|
|
116
|
+
compiled.set(property, fn);
|
|
117
|
+
}
|
|
118
|
+
return fn || void 0;
|
|
119
|
+
}
|
|
120
|
+
function compile(property, label = "property") {
|
|
121
|
+
const spec = property.normalize;
|
|
122
|
+
const entries = Array.isArray(spec) ? spec : [spec];
|
|
123
|
+
if (!isStringType(property.type)) throw new Error(g.f("{{normalize}} is only supported on string properties, %s is not a string", label));
|
|
124
|
+
const steps = entries.map(function(entry) {
|
|
125
|
+
if (typeof entry === "function") return entry;
|
|
126
|
+
const step = typeof entry === "string" && registry[entry];
|
|
127
|
+
if (!step) throw new Error(g.f("Unknown normalizer %j for %s", entry, label));
|
|
128
|
+
return step;
|
|
129
|
+
});
|
|
130
|
+
if (steps.length === 0) return null;
|
|
131
|
+
if (steps.length === 1) return steps[0];
|
|
132
|
+
return function normalize(value) {
|
|
133
|
+
let result = value;
|
|
134
|
+
for (let i = 0; i < steps.length; i++) {
|
|
135
|
+
result = steps[i](result);
|
|
136
|
+
if (typeof result !== "string") return result;
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Apply a compiled normalizer to a string, or in place to the string items of
|
|
143
|
+
* an array. Other values are returned unchanged.
|
|
144
|
+
*/
|
|
145
|
+
function normalizeValue(value, normalize) {
|
|
146
|
+
if (typeof value === "string") return normalize(value);
|
|
147
|
+
if (Array.isArray(value)) {
|
|
148
|
+
for (let i = 0; i < value.length; i++) if (typeof value[i] === "string") value[i] = normalize(value[i]);
|
|
149
|
+
}
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Normalize a plain data object in place, following embedded model
|
|
154
|
+
* definitions. Used where a write persists a data snapshot instead of the
|
|
155
|
+
* instance it validated.
|
|
156
|
+
* @param {Function} Model Model class owning `data`
|
|
157
|
+
* @param {Object} data Plain data keyed by property name
|
|
158
|
+
* @returns {Object} The same `data` object
|
|
159
|
+
*/
|
|
160
|
+
function normalizeData(Model, data) {
|
|
161
|
+
const properties = Model.definition.properties;
|
|
162
|
+
for (const key in data) {
|
|
163
|
+
const value = data[key];
|
|
164
|
+
const property = properties[key];
|
|
165
|
+
if (value == null || property === void 0) continue;
|
|
166
|
+
const normalize = getNormalizer(property);
|
|
167
|
+
if (normalize !== void 0) {
|
|
168
|
+
data[key] = normalizeValue(value, normalize);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const type = Array.isArray(property.type) ? property.type[0] : property.type;
|
|
172
|
+
if (typeof value !== "object" || !type || !type.definition) continue;
|
|
173
|
+
if (Array.isArray(value)) {
|
|
174
|
+
for (const item of value) if (item !== null && typeof item === "object") normalizeData(type, item);
|
|
175
|
+
} else normalizeData(type, value);
|
|
176
|
+
}
|
|
177
|
+
return data;
|
|
178
|
+
}
|
|
179
|
+
function isStringType(type) {
|
|
180
|
+
if (Array.isArray(type)) return type.length === 1 && type[0] === String;
|
|
181
|
+
return type === String;
|
|
182
|
+
}
|
|
183
|
+
module.exports = {
|
|
184
|
+
getNormalizer,
|
|
185
|
+
normalizeData,
|
|
186
|
+
normalizeValue,
|
|
187
|
+
registerNormalizer
|
|
188
|
+
};
|
|
189
|
+
}));
|
|
190
|
+
//#endregion
|
|
191
|
+
module.exports = require_normalizers();
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_globalize = require("./globalize.js");
|
|
4
|
+
//#region src/lib/property-constraints.ts
|
|
5
|
+
var require_property_constraints = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
/*!
|
|
7
|
+
* Declarative property constraints, using the JSON Schema keywords that
|
|
8
|
+
* loopback-swagger already publishes for a property:
|
|
9
|
+
*
|
|
10
|
+
* - strings: `minLength`, `maxLength` (Unicode characters), `pattern`, `format`
|
|
11
|
+
* - numbers: `minimum`/`min`, `maximum`/`max`
|
|
12
|
+
* - any primitive: `enum`
|
|
13
|
+
* - arrays: `minItems`, `maxItems`; the other keywords apply to each item
|
|
14
|
+
*
|
|
15
|
+
* Constraints only apply to present values. `null` and `undefined` pass;
|
|
16
|
+
* `required` decides whether a value must be present.
|
|
17
|
+
*/
|
|
18
|
+
const g = require_lib_globalize();
|
|
19
|
+
const EMAIL = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
20
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
21
|
+
const URI_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
22
|
+
const FORMATS = {
|
|
23
|
+
email: {
|
|
24
|
+
check: (s) => EMAIL.test(s),
|
|
25
|
+
message: "is not a valid email address"
|
|
26
|
+
},
|
|
27
|
+
uri: {
|
|
28
|
+
check: isUri,
|
|
29
|
+
message: "is not a valid URI"
|
|
30
|
+
},
|
|
31
|
+
uuid: {
|
|
32
|
+
check: (s) => UUID.test(s),
|
|
33
|
+
message: "is not a valid UUID"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
function isUri(value) {
|
|
37
|
+
if (!URI_SCHEME.test(value)) return false;
|
|
38
|
+
try {
|
|
39
|
+
new URL(value);
|
|
40
|
+
return true;
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function plural(count, noun) {
|
|
46
|
+
return `${count} ${noun}${count === 1 ? "" : "s"}`;
|
|
47
|
+
}
|
|
48
|
+
function exceedsLength(value, max) {
|
|
49
|
+
if (value.length <= max) return false;
|
|
50
|
+
let count = 0;
|
|
51
|
+
for (const _ of value) if (++count > max) return true;
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
function shorterThan(value, min) {
|
|
55
|
+
if (value.length < min) return true;
|
|
56
|
+
let count = 0;
|
|
57
|
+
for (const _ of value) if (++count >= min) return false;
|
|
58
|
+
return count < min;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Compile the constraints declared on a built property definition.
|
|
62
|
+
* Throws when a keyword does not fit the property type or has an invalid value.
|
|
63
|
+
* @param {Object} property Built property definition (with resolved `type`)
|
|
64
|
+
* @param {String} label `Model.property`, used in error messages
|
|
65
|
+
* @returns {Array} Constraints with `check(value)`, `code` and `message`
|
|
66
|
+
*/
|
|
67
|
+
function getPropertyConstraints(property, label) {
|
|
68
|
+
if (property == null || typeof property !== "object") return [];
|
|
69
|
+
const isArray = Array.isArray(property.type) || property.type === Array;
|
|
70
|
+
const itemType = Array.isArray(property.type) ? property.type[0] : isArray ? void 0 : property.type;
|
|
71
|
+
const constraints = [];
|
|
72
|
+
function invalid(message, ...args) {
|
|
73
|
+
return new Error(g.f("Invalid constraint on %s: " + message, label, ...args));
|
|
74
|
+
}
|
|
75
|
+
function count(keyword) {
|
|
76
|
+
const value = property[keyword];
|
|
77
|
+
if (!Number.isSafeInteger(value) || value < 0) throw invalid("{{%s}} must be a non-negative integer", keyword);
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
function limit(keyword, alias) {
|
|
81
|
+
const value = property[keyword];
|
|
82
|
+
const aliased = property[alias];
|
|
83
|
+
if (value != null && aliased != null && value !== aliased) throw invalid("{{%s}} and {{%s}} disagree", keyword, alias);
|
|
84
|
+
const resolved = value != null ? value : aliased;
|
|
85
|
+
if (resolved == null) return void 0;
|
|
86
|
+
if (typeof resolved !== "number" || !Number.isFinite(resolved)) throw invalid("{{%s}} must be a finite number", value != null ? keyword : alias);
|
|
87
|
+
return resolved;
|
|
88
|
+
}
|
|
89
|
+
function requireItemType(keyword, type, typeName) {
|
|
90
|
+
if (itemType !== type) throw invalid("{{%s}} only applies to %s properties", keyword, typeName);
|
|
91
|
+
}
|
|
92
|
+
function add(code, message, check) {
|
|
93
|
+
constraints.push({
|
|
94
|
+
code,
|
|
95
|
+
message,
|
|
96
|
+
check: isArray ? (value) => !Array.isArray(value) || value.every((item) => item == null || check(item)) : check
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (property.minLength != null) {
|
|
100
|
+
requireItemType("minLength", String, "string");
|
|
101
|
+
const min = count("minLength");
|
|
102
|
+
add("length.min", `must contain at least ${plural(min, "character")}`, (v) => !shorterThan(v, min));
|
|
103
|
+
}
|
|
104
|
+
if (property.maxLength != null) {
|
|
105
|
+
requireItemType("maxLength", String, "string");
|
|
106
|
+
const max = count("maxLength");
|
|
107
|
+
add("length.max", `must contain no more than ${plural(max, "character")}`, (v) => !exceedsLength(v, max));
|
|
108
|
+
}
|
|
109
|
+
if (property.pattern != null) {
|
|
110
|
+
requireItemType("pattern", String, "string");
|
|
111
|
+
let regex;
|
|
112
|
+
try {
|
|
113
|
+
regex = property.pattern instanceof RegExp ? property.pattern : new RegExp(property.pattern);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
throw invalid("{{pattern}} is not a valid regular expression: %s", err.message);
|
|
116
|
+
}
|
|
117
|
+
if (regex.global || regex.sticky) regex = new RegExp(regex.source, regex.flags.replace(/[gy]/g, ""));
|
|
118
|
+
add("format", "is invalid", (v) => regex.test(v));
|
|
119
|
+
}
|
|
120
|
+
if (property.format != null && FORMATS[property.format]) {
|
|
121
|
+
requireItemType("format", String, "string");
|
|
122
|
+
const format = FORMATS[property.format];
|
|
123
|
+
add("format." + property.format, format.message, format.check);
|
|
124
|
+
}
|
|
125
|
+
const minimum = limit("minimum", "min");
|
|
126
|
+
const maximum = limit("maximum", "max");
|
|
127
|
+
if (minimum !== void 0) {
|
|
128
|
+
requireItemType(property.minimum != null ? "minimum" : "min", Number, "number");
|
|
129
|
+
add("range.min", `must be greater than or equal to ${minimum}`, (v) => v >= minimum);
|
|
130
|
+
}
|
|
131
|
+
if (maximum !== void 0) {
|
|
132
|
+
requireItemType(property.maximum != null ? "maximum" : "max", Number, "number");
|
|
133
|
+
add("range.max", `must be less than or equal to ${maximum}`, (v) => v <= maximum);
|
|
134
|
+
}
|
|
135
|
+
if (property.enum != null) {
|
|
136
|
+
if (itemType !== String && itemType !== Number && itemType !== Boolean) throw invalid("{{enum}} only applies to string, number and boolean properties");
|
|
137
|
+
if (!Array.isArray(property.enum) || property.enum.length === 0) throw invalid("{{enum}} must be a non-empty array");
|
|
138
|
+
const itemTypeName = itemType.name.toLowerCase();
|
|
139
|
+
if (property.enum.some((value) => typeof value !== itemTypeName)) throw invalid("{{enum}} values must be of type %s", itemTypeName);
|
|
140
|
+
const allowed = new Set(property.enum);
|
|
141
|
+
add("inclusion", `must be one of: ${property.enum.join(", ")}`, (v) => allowed.has(v));
|
|
142
|
+
}
|
|
143
|
+
if (property.minItems != null || property.maxItems != null) {
|
|
144
|
+
if (!isArray) throw invalid("{{minItems}} and {{maxItems}} only apply to array properties");
|
|
145
|
+
}
|
|
146
|
+
if (property.minItems != null) {
|
|
147
|
+
const min = count("minItems");
|
|
148
|
+
constraints.push({
|
|
149
|
+
code: "items.min",
|
|
150
|
+
message: `must contain at least ${plural(min, "item")}`,
|
|
151
|
+
check: (v) => !Array.isArray(v) || v.length >= min
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (property.maxItems != null) {
|
|
155
|
+
const max = count("maxItems");
|
|
156
|
+
constraints.push({
|
|
157
|
+
code: "items.max",
|
|
158
|
+
message: `must contain no more than ${plural(max, "item")}`,
|
|
159
|
+
check: (v) => !Array.isArray(v) || v.length <= max
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return constraints;
|
|
163
|
+
}
|
|
164
|
+
module.exports = { getPropertyConstraints };
|
|
165
|
+
}));
|
|
166
|
+
//#endregion
|
|
167
|
+
module.exports = require_property_constraints();
|
package/dist/lib/validations.js
CHANGED
|
@@ -10,6 +10,8 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
10
10
|
*/
|
|
11
11
|
exports.ValidationError = ValidationError;
|
|
12
12
|
exports.Validatable = Validatable;
|
|
13
|
+
exports.addPropertyConstraints = addPropertyConstraints;
|
|
14
|
+
exports.checkPropertyConstraints = checkPropertyConstraints;
|
|
13
15
|
/**
|
|
14
16
|
* This class provides methods that add validation cababilities to models.
|
|
15
17
|
* Each of the validations runs when the `obj.isValid()` method is called.
|
|
@@ -354,6 +356,37 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
354
356
|
} else err();
|
|
355
357
|
}
|
|
356
358
|
/*!
|
|
359
|
+
* Declarative property constraint (see property-constraints.js). Only present
|
|
360
|
+
* values are checked; `required` handles presence.
|
|
361
|
+
*/
|
|
362
|
+
function validateConstraint(attr, conf, err, _options) {
|
|
363
|
+
const value = this[attr];
|
|
364
|
+
if (value != null && !conf.check(value)) err();
|
|
365
|
+
}
|
|
366
|
+
/*!
|
|
367
|
+
* Register compiled property constraints as validations of `attr`.
|
|
368
|
+
*/
|
|
369
|
+
function addPropertyConstraints(cls, attr, constraints) {
|
|
370
|
+
for (const constraint of constraints) configure(cls, "constraint", [attr, constraint]);
|
|
371
|
+
}
|
|
372
|
+
/*!
|
|
373
|
+
* Run only the property constraints of `attrs`, for partial writes that skip
|
|
374
|
+
* full validation. Fills `inst.errors` like isValid() and returns validity.
|
|
375
|
+
*/
|
|
376
|
+
function checkPropertyConstraints(inst, attrs) {
|
|
377
|
+
const validations = inst.constructor.validations || {};
|
|
378
|
+
const errors = new Errors();
|
|
379
|
+
let valid = true;
|
|
380
|
+
Object.defineProperty(inst, "errors", {
|
|
381
|
+
enumerable: false,
|
|
382
|
+
configurable: true,
|
|
383
|
+
value: errors
|
|
384
|
+
});
|
|
385
|
+
for (const attr of attrs) for (const conf of validations[attr] || []) if (conf.validation === "constraint" && validationFailed(inst, attr, conf, {})) valid = false;
|
|
386
|
+
if (valid) cleanErrors(inst);
|
|
387
|
+
return valid;
|
|
388
|
+
}
|
|
389
|
+
/*!
|
|
357
390
|
* Custom validator
|
|
358
391
|
*/
|
|
359
392
|
function validateCustom(attr, conf, err, options, done) {
|
|
@@ -451,6 +484,7 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
|
|
|
451
484
|
exclusion: validateExclusion,
|
|
452
485
|
format: validateFormat,
|
|
453
486
|
custom: validateCustom,
|
|
487
|
+
constraint: validateConstraint,
|
|
454
488
|
uniqueness: validateUniqueness,
|
|
455
489
|
date: validateDate
|
|
456
490
|
};
|
package/dist/package.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var require_package = /* @__PURE__ */ require("./_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
3
3
|
module.exports = {
|
|
4
4
|
"name": "@vsaas/loopback-datasource-juggler",
|
|
5
|
-
"version": "13.0
|
|
5
|
+
"version": "13.1.0",
|
|
6
6
|
"description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"Connector",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vsaas/loopback-datasource-juggler",
|
|
3
|
-
"version": "13.0
|
|
3
|
+
"version": "13.1.0",
|
|
4
4
|
"description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Connector",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"inflection": "^3.0.2",
|
|
58
58
|
"minimatch": "^10.2.6",
|
|
59
59
|
"neotraverse": "^0.6.18",
|
|
60
|
-
"qs": "^6.
|
|
60
|
+
"qs": "^6.16.0",
|
|
61
61
|
"@vsaas/loopback-connector": "^12.0.1"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
package/types/model.d.ts
CHANGED
|
@@ -13,12 +13,76 @@ import { Listener, OperationHookContext, SyncListener } from './observer-mixin';
|
|
|
13
13
|
*/
|
|
14
14
|
export type PropertyType = string | Function | { [property: string]: PropertyType };
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Built-in property normalizers
|
|
18
|
+
*/
|
|
19
|
+
export type BuiltinNormalizer =
|
|
20
|
+
| 'trim'
|
|
21
|
+
| 'lowercase'
|
|
22
|
+
| 'uppercase'
|
|
23
|
+
| 'nfc'
|
|
24
|
+
| 'unaccent'
|
|
25
|
+
| 'collapseSpaces'
|
|
26
|
+
| 'emptyToNull'
|
|
27
|
+
| 'stripInvisible'
|
|
28
|
+
| 'numeric'
|
|
29
|
+
| 'alphanumeric'
|
|
30
|
+
| 'stripHtml';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A normalizer step. Returning null or undefined stops the pipeline and stores
|
|
34
|
+
* that value.
|
|
35
|
+
*/
|
|
36
|
+
export type NormalizerFunction = (value: string) => string | null | undefined;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A built-in or registered normalizer name, or an inline normalizer
|
|
40
|
+
*/
|
|
41
|
+
export type PropertyNormalizer = BuiltinNormalizer | (string & {}) | NormalizerFunction;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Register a named normalizer usable from property definitions.
|
|
45
|
+
* Register it before defining the models that reference it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function registerNormalizer(name: string, fn: NormalizerFunction): void;
|
|
48
|
+
|
|
16
49
|
/**
|
|
17
50
|
* Property definition
|
|
18
51
|
*/
|
|
19
52
|
export interface PropertyDefinition extends AnyObject {
|
|
20
53
|
type?: PropertyType;
|
|
21
54
|
id?: boolean | number;
|
|
55
|
+
/**
|
|
56
|
+
* Normalize string values, in order, when they are assigned and when they
|
|
57
|
+
* are compared for equality in `where` filters. Only for string and string
|
|
58
|
+
* array properties.
|
|
59
|
+
*/
|
|
60
|
+
normalize?: PropertyNormalizer | PropertyNormalizer[] | false | null;
|
|
61
|
+
/** Minimum length in Unicode characters; string properties */
|
|
62
|
+
minLength?: number;
|
|
63
|
+
/** Maximum length in Unicode characters; string properties */
|
|
64
|
+
maxLength?: number;
|
|
65
|
+
/** Regular expression the value must match; string properties */
|
|
66
|
+
pattern?: string | RegExp;
|
|
67
|
+
/**
|
|
68
|
+
* `email`, `uri` and `uuid` are validated on string properties; other
|
|
69
|
+
* formats are documentation only
|
|
70
|
+
*/
|
|
71
|
+
format?: string;
|
|
72
|
+
/** Inclusive lower bound; number properties */
|
|
73
|
+
minimum?: number;
|
|
74
|
+
/** Alias of `minimum` */
|
|
75
|
+
min?: number;
|
|
76
|
+
/** Inclusive upper bound; number properties */
|
|
77
|
+
maximum?: number;
|
|
78
|
+
/** Alias of `maximum` */
|
|
79
|
+
max?: number;
|
|
80
|
+
/** Allowed values; string, number and boolean properties */
|
|
81
|
+
enum?: Array<string | number | boolean>;
|
|
82
|
+
/** Minimum number of items; array properties */
|
|
83
|
+
minItems?: number;
|
|
84
|
+
/** Maximum number of items; array properties */
|
|
85
|
+
maxItems?: number;
|
|
22
86
|
}
|
|
23
87
|
|
|
24
88
|
/**
|