@appweaver/core 1.3.1 → 1.4.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/export/export-service.d.ts +4 -3
- package/export/export-service.js +33 -22
- package/factory/create-model.js +36 -21
- package/factory/create-service.js +5 -3
- package/package.json +2 -2
- package/prisma/client/commonInputTypes.d.ts +0 -50
- package/prisma/client/internal/class.js +3 -3
- package/prisma/client/models/File.d.ts +15 -28
- package/resource/resource-loader.js +6 -0
- package/resource/resource-routes.js +2 -2
- package/resource/resource-schema.d.ts +38 -12
- package/resource/resource-schema.js +63 -15
- package/resource/resource-service.d.ts +25 -16
- package/resource/resource-service.js +61 -26
- package/resource/schemas/resource-sort-schema.js +2 -2
- package/resource/utils/cursor-util.d.ts +60 -0
- package/resource/utils/cursor-util.js +117 -0
- package/resource/utils/index.d.ts +1 -0
- package/resource/utils/index.js +1 -0
- package/resource/utils/relation-util.d.ts +3 -3
- package/resource/utils/relation-util.js +1 -1
- package/resource/utils/sort-util.d.ts +11 -0
- package/resource/utils/sort-util.js +18 -0
- package/security/api-key/api-key-auth.js +4 -1
- package/security/auth-service.d.ts +7 -7
- package/security/auth-service.js +43 -5
- package/security/create-auth-resources.d.ts +2 -1
- package/security/oauth2/create-oauth2-plugin.js +3 -34
- package/security/oauth2/oauth2-microsoft.js +1 -1
- package/security/oauth2/oauth2-util.d.ts +9 -7
- package/security/oauth2/oauth2-util.js +17 -10
- package/security/resources/api-key/model.js +2 -0
- package/security/resources/connected-account/model.js +2 -0
- package/security/store/database-security-store.js +2 -1
- package/storage/file-service.d.ts +19 -4
- package/storage/file-service.js +199 -146
- package/storage/resources/file/model.js +4 -2
- package/types/auth.d.ts +8 -8
- package/types/generated.d.ts +8 -2
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/storage.d.ts +16 -0
- package/types/storage.js +2 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/model-util.d.ts +12 -0
- package/utils/model-util.js +113 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateScalarDefaults = validateScalarDefaults;
|
|
4
|
+
const common_1 = require("@appweaver/common");
|
|
5
|
+
/**
|
|
6
|
+
* Validates that the default value of every scalar and virtual field satisfies
|
|
7
|
+
* the constraints declared on that same field. A default outside its own
|
|
8
|
+
* constraints produces records that do not match the output schema of their
|
|
9
|
+
* model, which only surfaces once the model is nested in a nullable relation,
|
|
10
|
+
* where the response serializer validates it before picking a schema branch.
|
|
11
|
+
*
|
|
12
|
+
* @param {Record<string, ResourceModel>} models - All loaded models keyed by name.
|
|
13
|
+
* @throws {Error} When any default value violates the constraints of its field.
|
|
14
|
+
*/
|
|
15
|
+
function validateScalarDefaults(models) {
|
|
16
|
+
const errors = [];
|
|
17
|
+
for (const model of Object.values(models)) {
|
|
18
|
+
const fields = {
|
|
19
|
+
...(model.config.scalars ?? {}),
|
|
20
|
+
...(model.config.virtual ?? {})
|
|
21
|
+
};
|
|
22
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
23
|
+
for (const error of defaultValueErrors(field)) {
|
|
24
|
+
errors.push(`'${model.name}.${name}' ${error}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (errors.length > 0) {
|
|
29
|
+
throw new Error(`Invalid resource model default values:\n${errors
|
|
30
|
+
.map((error) => ` - ${error}`)
|
|
31
|
+
.join('\n')}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** @internal */
|
|
35
|
+
function defaultValueErrors(field) {
|
|
36
|
+
if (field.default === undefined) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
// An array field defaults to a list of values, each validated on its own
|
|
40
|
+
const values = field.array
|
|
41
|
+
? (0, common_1.isArray)(field.default)
|
|
42
|
+
? field.default
|
|
43
|
+
: [field.default]
|
|
44
|
+
: [field.default];
|
|
45
|
+
return values.flatMap((value) => defaultErrors(field, value));
|
|
46
|
+
}
|
|
47
|
+
/** @internal */
|
|
48
|
+
function defaultErrors(field, value) {
|
|
49
|
+
const errors = [];
|
|
50
|
+
const invalidType = (expected) => `has a default value of ${JSON.stringify(value)}, which is not ${expected}`;
|
|
51
|
+
const violates = (constraint, limit) => `has a default value of ${JSON.stringify(value)}, which violates its ` +
|
|
52
|
+
`own ${constraint} of ${JSON.stringify(limit)}`;
|
|
53
|
+
switch (field.type) {
|
|
54
|
+
case 'string':
|
|
55
|
+
if (!(0, common_1.isString)(value)) {
|
|
56
|
+
return [invalidType('a string')];
|
|
57
|
+
}
|
|
58
|
+
if (field.minLength !== undefined && value.length < field.minLength) {
|
|
59
|
+
errors.push(violates('minLength', field.minLength));
|
|
60
|
+
}
|
|
61
|
+
if (field.maxLength !== undefined && value.length > field.maxLength) {
|
|
62
|
+
errors.push(violates('maxLength', field.maxLength));
|
|
63
|
+
}
|
|
64
|
+
if (field.pattern !== undefined &&
|
|
65
|
+
!matchesPattern(value, field.pattern)) {
|
|
66
|
+
errors.push(violates('pattern', field.pattern));
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case 'int':
|
|
70
|
+
case 'bigInt':
|
|
71
|
+
case 'float':
|
|
72
|
+
if (!(0, common_1.isNumber)(value)) {
|
|
73
|
+
return [invalidType('a number')];
|
|
74
|
+
}
|
|
75
|
+
if (field.type !== 'float' && !Number.isInteger(value)) {
|
|
76
|
+
return [invalidType('an integer')];
|
|
77
|
+
}
|
|
78
|
+
if (field.minimum !== undefined && value < field.minimum) {
|
|
79
|
+
errors.push(violates('minimum', field.minimum));
|
|
80
|
+
}
|
|
81
|
+
if (field.maximum !== undefined && value > field.maximum) {
|
|
82
|
+
errors.push(violates('maximum', field.maximum));
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
case 'boolean':
|
|
86
|
+
if (!(0, common_1.isBoolean)(value)) {
|
|
87
|
+
errors.push(invalidType('a boolean'));
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
case 'dateTime':
|
|
91
|
+
if (!(value instanceof Date) &&
|
|
92
|
+
(!(0, common_1.isString)(value) || Number.isNaN(Date.parse(value)))) {
|
|
93
|
+
errors.push(invalidType('a valid date'));
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case 'enum':
|
|
97
|
+
if (!(field.values ?? []).includes(value)) {
|
|
98
|
+
errors.push(violates('allowed values', field.values ?? []));
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
return errors;
|
|
103
|
+
}
|
|
104
|
+
/** Tests a value against a field pattern, skipping patterns that are not valid
|
|
105
|
+
* regular expressions since they cannot be evaluated. @internal */
|
|
106
|
+
function matchesPattern(value, pattern) {
|
|
107
|
+
try {
|
|
108
|
+
return new RegExp(pattern).test(value);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|