lucid-extension-sdk 0.0.356 → 0.0.357
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/core/guards.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export type DestructureGuardedTypeObj<Obj extends {
|
|
|
7
7
|
}>;
|
|
8
8
|
export type Validator<TO extends FROM, FROM = unknown> = (p1: FROM) => p1 is TO;
|
|
9
9
|
export type ValidatorWithTracking<TO extends FROM, FROM = unknown> = (p1: FROM, p2?: Map<number, unknown[]>, p3?: number) => p1 is TO;
|
|
10
|
-
export type Pruner = (p1: unknown, p2?: Map<number, unknown[]>, p3?: number) => unknown;
|
|
10
|
+
export type Pruner = (p1: unknown, p2?: Map<number, unknown[]>, p3?: number, key?: string | undefined) => unknown;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Pruner } from '../guards';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a pruner function that will remove object fields that are invalid.
|
|
4
|
-
* If a sub-pruner is provided, then the object values
|
|
5
|
-
*
|
|
4
|
+
* If a sub-pruner is provided for a field, then the object field values will be pruned.
|
|
5
|
+
* If no pruner is provided for field, nothing happens to that field.
|
|
6
6
|
*
|
|
7
7
|
* @returns A pruner function that takes data and invalid fields. If data is not an
|
|
8
8
|
* object, it will return the data as is instead of pruning.
|
|
@@ -19,3 +19,4 @@ export declare function objectPruner(subPruners: {
|
|
|
19
19
|
* array, it will return the data as is instead of pruning.
|
|
20
20
|
*/
|
|
21
21
|
export declare function arrayPruner(subPruner?: Pruner | undefined): Pruner;
|
|
22
|
+
export declare function pruneObjectField(): Pruner;
|
package/core/pruners/pruners.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.arrayPruner = exports.objectPruner = void 0;
|
|
3
|
+
exports.pruneObjectField = exports.arrayPruner = exports.objectPruner = void 0;
|
|
4
4
|
const checks_1 = require("../checks");
|
|
5
5
|
/**
|
|
6
6
|
* Creates a pruner function that will remove object fields that are invalid.
|
|
7
|
-
* If a sub-pruner is provided, then the object values
|
|
8
|
-
*
|
|
7
|
+
* If a sub-pruner is provided for a field, then the object field values will be pruned.
|
|
8
|
+
* If no pruner is provided for field, nothing happens to that field.
|
|
9
9
|
*
|
|
10
10
|
* @returns A pruner function that takes data and invalid fields. If data is not an
|
|
11
11
|
* object, it will return the data as is instead of pruning.
|
|
@@ -15,20 +15,11 @@ function objectPruner(subPruners) {
|
|
|
15
15
|
if ((0, checks_1.isArray)(data) || !(0, checks_1.isObjectUnsafe)(data)) {
|
|
16
16
|
return data;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
else {
|
|
24
|
-
Object.keys(data).forEach((key) => {
|
|
25
|
-
var _a;
|
|
26
|
-
if ((_a = invalidFields === null || invalidFields === void 0 ? void 0 : invalidFields.get(level)) === null || _a === void 0 ? void 0 : _a.includes(key)) {
|
|
27
|
-
data[key] = undefined;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
return data;
|
|
18
|
+
const dataToBePruned = Object.assign({}, data);
|
|
19
|
+
Object.entries(subPruners).forEach(([key, pruner]) => {
|
|
20
|
+
dataToBePruned[key] = pruner(dataToBePruned[key], invalidFields, level + 1, key);
|
|
21
|
+
});
|
|
22
|
+
return dataToBePruned;
|
|
32
23
|
};
|
|
33
24
|
}
|
|
34
25
|
exports.objectPruner = objectPruner;
|
|
@@ -46,7 +37,19 @@ function arrayPruner(subPruner) {
|
|
|
46
37
|
return data;
|
|
47
38
|
}
|
|
48
39
|
if (subPruner) {
|
|
49
|
-
return data.
|
|
40
|
+
return data.reduce((acc, item) => {
|
|
41
|
+
var _a;
|
|
42
|
+
const prunedItem = subPruner(item, invalidElements, level + 1);
|
|
43
|
+
/* Here we are relying on the fact that the pruner will return the same object reference, if it is not
|
|
44
|
+
pruned, but will return a new object reference, if it is pruned. This is a bit of a hack but
|
|
45
|
+
TODO EI 50 we're overhauling how this works in future
|
|
46
|
+
*/
|
|
47
|
+
if (prunedItem === item && ((_a = invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.get(level)) === null || _a === void 0 ? void 0 : _a.includes(item))) {
|
|
48
|
+
return acc;
|
|
49
|
+
}
|
|
50
|
+
acc.push(prunedItem);
|
|
51
|
+
return acc;
|
|
52
|
+
}, []);
|
|
50
53
|
}
|
|
51
54
|
else {
|
|
52
55
|
return data.filter((item) => { var _a; return !((_a = invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.get(level)) === null || _a === void 0 ? void 0 : _a.includes(item)); });
|
|
@@ -54,3 +57,24 @@ function arrayPruner(subPruner) {
|
|
|
54
57
|
};
|
|
55
58
|
}
|
|
56
59
|
exports.arrayPruner = arrayPruner;
|
|
60
|
+
/*
|
|
61
|
+
* Creates a pruner function to be passed in with the object pruner that will remove the field if it is invalid.
|
|
62
|
+
*/
|
|
63
|
+
function pruneObjectField() {
|
|
64
|
+
return (data, invalidFields, level = 0, key = undefined) => {
|
|
65
|
+
var _a;
|
|
66
|
+
// When this is called as a part of objectPruner, level will be atleast 1
|
|
67
|
+
if ((0, checks_1.isArray)(data) || (0, checks_1.isObjectUnsafe)(data) || level <= 0) {
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
const invalidFieldAtCurrentLevel = (_a = invalidFields === null || invalidFields === void 0 ? void 0 : invalidFields.get(level - 1)) === null || _a === void 0 ? void 0 : _a.filter(checks_1.isArray);
|
|
71
|
+
if (!(0, checks_1.isDef)(invalidFieldAtCurrentLevel)) {
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
if (invalidFieldAtCurrentLevel.some((value) => value[0] === key && value[1] === data)) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return data;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.pruneObjectField = pruneObjectField;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isNumber, isString } from '../checks';
|
|
2
|
-
import { DestructureGuardedTypeObj, GuardToType, Validator } from '../guards';
|
|
2
|
+
import { DestructureGuardedTypeObj, GuardToType, Validator, ValidatorWithTracking } from '../guards';
|
|
3
3
|
/*********************************************************************************
|
|
4
4
|
* Validator generators: These functions construct new composite validators
|
|
5
5
|
*from elemental validators.
|
|
@@ -104,6 +104,11 @@ export declare function strictObjectValidator<T extends {
|
|
|
104
104
|
export declare function recordValidator<K extends string, V>(keyList: K[], valueValidator: Validator<V>): (x: unknown) => x is Record<K, V>;
|
|
105
105
|
export declare function typedRecordValidator<K extends string | number | symbol, V>(keyValidator: Validator<K>, valueValidator: Validator<V>): (x: unknown) => x is Record<K, V>;
|
|
106
106
|
export declare function objectOfValidator<T>(subValidator: Validator<T>): (x: unknown) => x is Record<any, T>;
|
|
107
|
+
/**
|
|
108
|
+
* This validator functions the same as {@link objectOfValidator}, with the option of passing in a map
|
|
109
|
+
* which will track all of the fields that were found to be invalid.
|
|
110
|
+
*/
|
|
111
|
+
export declare function objectOfValidatorWithInvalidFieldTracking<T>(subValidator: ValidatorWithTracking<T>): (subject: unknown, invalidFields?: Map<number, unknown[]>, level?: number) => subject is Record<any, T>;
|
|
107
112
|
/**
|
|
108
113
|
* Create a validator which allows the target to be either null or satisfy the
|
|
109
114
|
* sub-validator.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.asAssertion = exports.validatorWithMessage = exports.minLengthValidator = exports.maxLengthValidator = exports.isDate = exports.isPositiveNumber = exports.isSize = exports.isPanelSize = exports.isBoundingBox = exports.isPointLike = exports.isOpacity = exports.isFalse = exports.isTrue = exports.isFlag = exports.isRestrictions = exports.isStringOrNegativeOne = exports.isBooleanOrEmptyString = exports.isNumberOrEmptyString = exports.isSet = exports.propertyValidator = exports.exclude = exports.both = exports.either = exports.isNullOption = exports.nullableOption = exports.option = exports.nullableValidatorWithInvalidElementTracking = exports.nullable = exports.objectOfValidator = exports.typedRecordValidator = exports.recordValidator = exports.strictObjectValidator = exports.partialObjectValidator = exports.objectValidatorWithInvalidFieldTracking = exports.objectValidator = exports.mapValidator = exports.someValidator = exports.someValue = exports.tupleValidator = exports.arrayValidatorWithInvalidElementTracking = exports.arrayValidator = exports.rangeValidator = exports.enumValidator = exports.stringEnumValidator = void 0;
|
|
3
|
+
exports.asAssertion = exports.validatorWithMessage = exports.minLengthValidator = exports.maxLengthValidator = exports.isDate = exports.isPositiveNumber = exports.isSize = exports.isPanelSize = exports.isBoundingBox = exports.isPointLike = exports.isOpacity = exports.isFalse = exports.isTrue = exports.isFlag = exports.isRestrictions = exports.isStringOrNegativeOne = exports.isBooleanOrEmptyString = exports.isNumberOrEmptyString = exports.isSet = exports.propertyValidator = exports.exclude = exports.both = exports.either = exports.isNullOption = exports.nullableOption = exports.option = exports.nullableValidatorWithInvalidElementTracking = exports.nullable = exports.objectOfValidatorWithInvalidFieldTracking = exports.objectOfValidator = exports.typedRecordValidator = exports.recordValidator = exports.strictObjectValidator = exports.partialObjectValidator = exports.objectValidatorWithInvalidFieldTracking = exports.objectValidator = exports.mapValidator = exports.someValidator = exports.someValue = exports.tupleValidator = exports.arrayValidatorWithInvalidElementTracking = exports.arrayValidator = exports.rangeValidator = exports.enumValidator = exports.stringEnumValidator = void 0;
|
|
4
4
|
const checks_1 = require("../checks");
|
|
5
5
|
/*********************************************************************************
|
|
6
6
|
* Validator generators: These functions construct new composite validators
|
|
@@ -173,6 +173,7 @@ exports.objectValidator = objectValidator;
|
|
|
173
173
|
*/
|
|
174
174
|
function objectValidatorWithInvalidFieldTracking(validatorStructure) {
|
|
175
175
|
return (subject, invalidFields, level = 0) => {
|
|
176
|
+
var _a;
|
|
176
177
|
if ((0, checks_1.isArray)(subject) || !(0, checks_1.isObjectUnsafe)(subject)) {
|
|
177
178
|
return false;
|
|
178
179
|
}
|
|
@@ -186,7 +187,8 @@ function objectValidatorWithInvalidFieldTracking(validatorStructure) {
|
|
|
186
187
|
}
|
|
187
188
|
});
|
|
188
189
|
if ((0, checks_1.isMap)(invalidFields)) {
|
|
189
|
-
invalidFields.
|
|
190
|
+
const prevInvalidList = (_a = invalidFields.get(level)) !== null && _a !== void 0 ? _a : [];
|
|
191
|
+
invalidFields.set(level, prevInvalidList.concat(invalidList));
|
|
190
192
|
}
|
|
191
193
|
return valid;
|
|
192
194
|
}
|
|
@@ -257,6 +259,32 @@ function objectOfValidator(subValidator) {
|
|
|
257
259
|
};
|
|
258
260
|
}
|
|
259
261
|
exports.objectOfValidator = objectOfValidator;
|
|
262
|
+
/**
|
|
263
|
+
* This validator functions the same as {@link objectOfValidator}, with the option of passing in a map
|
|
264
|
+
* which will track all of the fields that were found to be invalid.
|
|
265
|
+
*/
|
|
266
|
+
function objectOfValidatorWithInvalidFieldTracking(subValidator) {
|
|
267
|
+
return (subject, invalidFields, level = 0) => {
|
|
268
|
+
var _a;
|
|
269
|
+
if ((0, checks_1.isArray)(subject) || !(0, checks_1.isObjectUnsafe)(subject)) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
let valid = true;
|
|
273
|
+
const invalidList = [];
|
|
274
|
+
Object.entries(subject).forEach(([key, val]) => {
|
|
275
|
+
if (!subValidator(val, invalidFields, level + 1)) {
|
|
276
|
+
invalidList.push([key, val]);
|
|
277
|
+
valid = false;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
if ((0, checks_1.isMap)(invalidFields)) {
|
|
281
|
+
const prevInvalidList = (_a = invalidFields.get(level)) !== null && _a !== void 0 ? _a : [];
|
|
282
|
+
invalidFields.set(level, prevInvalidList.concat(invalidList));
|
|
283
|
+
}
|
|
284
|
+
return valid;
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
exports.objectOfValidatorWithInvalidFieldTracking = objectOfValidatorWithInvalidFieldTracking;
|
|
260
288
|
/**
|
|
261
289
|
* Create a validator which allows the target to be either null or satisfy the
|
|
262
290
|
* sub-validator.
|