joist-orm 1.258.0 → 1.260.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/build/rules.d.ts +21 -7
- package/build/rules.d.ts.map +1 -1
- package/build/rules.js +59 -46
- package/build/rules.js.map +1 -1
- package/package.json +2 -2
package/build/rules.d.ts
CHANGED
|
@@ -36,6 +36,11 @@ export type GenericError = {
|
|
|
36
36
|
export type ValidationError = {
|
|
37
37
|
entity: Entity;
|
|
38
38
|
} & GenericError;
|
|
39
|
+
export type ValidationRuleOptsFn<T extends Entity> = (entity: T) => MaybePromise<boolean>;
|
|
40
|
+
export type ValidationRuleOpts<T extends Entity> = {
|
|
41
|
+
unless?: ValidationRuleOptsFn<T>;
|
|
42
|
+
if?: ValidationRuleOptsFn<T>;
|
|
43
|
+
};
|
|
39
44
|
export declare class ValidationErrors extends Error {
|
|
40
45
|
errors: Array<GenericError | ValidationError>;
|
|
41
46
|
readonly toJSON: () => string;
|
|
@@ -47,44 +52,51 @@ export declare class ValidationErrors extends Error {
|
|
|
47
52
|
*
|
|
48
53
|
* This is added automatically by codegen to entities based on FK not-nulls.
|
|
49
54
|
*/
|
|
50
|
-
export declare function newRequiredRule<T extends Entity>(key: keyof FieldsOf<T> & string): ValidationRule<T>;
|
|
55
|
+
export declare function newRequiredRule<T extends Entity>(key: keyof FieldsOf<T> & string, opts?: ValidationRuleOpts<T>): ValidationRule<T>;
|
|
51
56
|
/**
|
|
52
57
|
* Creates a validation rule that a field cannot be updated; it can only be set on creation.
|
|
53
58
|
*
|
|
54
59
|
* If the optional `unless` function returns true, then the update is allowed.
|
|
55
60
|
*/
|
|
56
|
-
export declare function cannotBeUpdated<T extends Entity, K extends keyof Changes<T> & string>(field: K,
|
|
61
|
+
export declare function cannotBeUpdated<T extends Entity, K extends keyof Changes<T> & string>(field: K, opts?: ValidationRuleOpts<T>): CannotBeUpdatedRule<T>;
|
|
62
|
+
export declare function cannotBeUpdated<T extends Entity, K extends keyof Changes<T> & string>(field: K, unless?: ValidationRuleOptsFn<T>): CannotBeUpdatedRule<T>;
|
|
57
63
|
type CannotBeUpdatedRule<T extends Entity> = ValidationRule<T> & {
|
|
58
64
|
field: string;
|
|
59
65
|
immutable: boolean;
|
|
60
66
|
};
|
|
61
67
|
export declare function isCannotBeUpdatedRule(rule: Function): rule is CannotBeUpdatedRule<any>;
|
|
62
68
|
/** For STI, enforces subtype-specific relations/FKs at runtime. */
|
|
63
|
-
export declare function mustBeSubType<T extends Entity, K extends keyof Changes<T> & string>(relationName: K): ValidationRule<T>;
|
|
69
|
+
export declare function mustBeSubType<T extends Entity, K extends keyof Changes<T> & string>(relationName: K, opts?: ValidationRuleOpts<T>): ValidationRule<T>;
|
|
64
70
|
/**
|
|
65
71
|
* Creates a validation rule that a field value cannot be smaller than a
|
|
66
72
|
* certain value.
|
|
67
73
|
*
|
|
68
74
|
* @param field The field to validate
|
|
69
75
|
* @param minValue The inclusive minimum value. e.g 0 range is [0, Infinity]
|
|
76
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
77
|
+
* - unless: Function that returns true to skip validation
|
|
78
|
+
* - if: Function that returns true to run validation
|
|
70
79
|
*
|
|
71
80
|
* @example
|
|
72
81
|
* // Age cannot be smaller than 0
|
|
73
82
|
* config.addRule(minValueRule("age", 0));
|
|
74
83
|
*/
|
|
75
|
-
export declare function minValueRule<T extends Entity, K extends keyof T & string>(field: K, minValue: number): ValidationRule<T>;
|
|
84
|
+
export declare function minValueRule<T extends Entity, K extends keyof T & string>(field: K, minValue: number, opts?: ValidationRuleOpts<T>): ValidationRule<T>;
|
|
76
85
|
/**
|
|
77
86
|
* Creates a validation rule that a field value cannot be greater than a
|
|
78
87
|
* certain value.
|
|
79
88
|
*
|
|
80
89
|
* @param field The field to validate
|
|
81
90
|
* @param maxValue The include maximum value. e.g 10 range is [-Infinity, 10]
|
|
91
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
92
|
+
* - unless: Function that returns true to skip validation
|
|
93
|
+
* - if: Function that returns true to run validation
|
|
82
94
|
*
|
|
83
95
|
* @example
|
|
84
96
|
* // Age cannot be greater than 150
|
|
85
97
|
* config.addRule(maxValueRule("age", 150));
|
|
86
98
|
*/
|
|
87
|
-
export declare function maxValueRule<T extends Entity, K extends keyof T & string>(field: K, maxValue: number): ValidationRule<T>;
|
|
99
|
+
export declare function maxValueRule<T extends Entity, K extends keyof T & string>(field: K, maxValue: number, opts?: ValidationRuleOpts<T>): ValidationRule<T>;
|
|
88
100
|
/**
|
|
89
101
|
* Creates a validation rule that a field value must be within a range of
|
|
90
102
|
* certain values
|
|
@@ -92,11 +104,13 @@ export declare function maxValueRule<T extends Entity, K extends keyof T & strin
|
|
|
92
104
|
* @param field The field to validate
|
|
93
105
|
* @param minValue The inclusive minimum value. e.g 0 [0, Infinity]
|
|
94
106
|
* @param maxValue The include maximum value. e.g 10 [-Infinity, 10]
|
|
107
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
108
|
+
* - unless: Function that returns true to skip validation
|
|
109
|
+
* - if: Function that returns true to run validation * @example
|
|
95
110
|
*
|
|
96
|
-
* @example
|
|
97
111
|
* // Age must be between 0 and 150
|
|
98
112
|
* config.addRule(rangeValueRule("age", 0, 150));
|
|
99
113
|
*/
|
|
100
|
-
export declare function rangeValueRule<T extends Entity, K extends keyof T & string>(field: K, minValue: number, maxValue: number): ValidationRule<T>;
|
|
114
|
+
export declare function rangeValueRule<T extends Entity, K extends keyof T & string>(field: K, minValue: number, maxValue: number, opts?: ValidationRuleOpts<T>): ValidationRule<T>;
|
|
101
115
|
export {};
|
|
102
116
|
//# sourceMappingURL=rules.d.ts.map
|
package/build/rules.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAiB,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,M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20
|
ValidationCode["maxValue"] = "over-max";
|
|
21
21
|
})(ValidationCode || (exports.ValidationCode = ValidationCode = {}));
|
|
22
|
+
function errorMessage(errors) {
|
|
23
|
+
if (errors.length === 1) {
|
|
24
|
+
return `Validation error: ${errors[0].entity.toString()} ${errors[0].message}`;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const message = [...(0, utils_1.groupBy)(errors, (e) => e.entity.toString()).entries()]
|
|
28
|
+
.map(([entityToString, errors]) => `${entityToString} ${errors.map((e) => e.message).join(", ")}`)
|
|
29
|
+
.join(", ");
|
|
30
|
+
return `Validation errors (${errors.length}): ${message}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
22
33
|
class ValidationErrors extends Error {
|
|
23
34
|
errors;
|
|
24
35
|
toJSON;
|
|
@@ -31,13 +42,27 @@ class ValidationErrors extends Error {
|
|
|
31
42
|
}
|
|
32
43
|
}
|
|
33
44
|
exports.ValidationErrors = ValidationErrors;
|
|
45
|
+
function ruleWithOpts(opts, fn) {
|
|
46
|
+
return (entity) => {
|
|
47
|
+
// Since all of our rules are sync, we run the rule first, then check if we should have run it second since this
|
|
48
|
+
// may be an async operation. This way we can avoid running promises if we don't have to.
|
|
49
|
+
const result = fn(entity);
|
|
50
|
+
if (result === undefined)
|
|
51
|
+
return;
|
|
52
|
+
const { unless, if: _if } = opts;
|
|
53
|
+
if (unless === undefined && _if === undefined)
|
|
54
|
+
return result;
|
|
55
|
+
const shouldRun = [unless ?? (() => false), _if ?? (() => true)].map((fn) => fn(entity));
|
|
56
|
+
return (0, utils_1.maybePromiseThen)(shouldRun.some((r) => r instanceof Promise) ? Promise.all(shouldRun) : shouldRun, ([unless, _if]) => (!unless && _if ? result : undefined));
|
|
57
|
+
};
|
|
58
|
+
}
|
|
34
59
|
/**
|
|
35
60
|
* Creates a validation rule for required fields.
|
|
36
61
|
*
|
|
37
62
|
* This is added automatically by codegen to entities based on FK not-nulls.
|
|
38
63
|
*/
|
|
39
|
-
function newRequiredRule(key) {
|
|
40
|
-
return (entity) => {
|
|
64
|
+
function newRequiredRule(key, opts = {}) {
|
|
65
|
+
return ruleWithOpts(opts, (entity) => {
|
|
41
66
|
// Use getField so that we peer through relations
|
|
42
67
|
if ((0, fields_1.getField)(entity, key) === undefined) {
|
|
43
68
|
// Sanity check for ReactiveQueryFields--this should be cheap to just always do; alternatively we could
|
|
@@ -48,53 +73,33 @@ function newRequiredRule(key) {
|
|
|
48
73
|
}
|
|
49
74
|
return { field: key, code: ValidationCode.required, message: `${key} is required` };
|
|
50
75
|
}
|
|
51
|
-
};
|
|
76
|
+
});
|
|
52
77
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
* If the optional `unless` function returns true, then the update is allowed.
|
|
57
|
-
*/
|
|
58
|
-
function cannotBeUpdated(field, unless) {
|
|
59
|
-
const fn = async (entity) => {
|
|
78
|
+
function cannotBeUpdated(field, optsOrUnless) {
|
|
79
|
+
const opts = typeof optsOrUnless === "function" ? { unless: optsOrUnless } : (optsOrUnless ?? {});
|
|
80
|
+
const fn = ruleWithOpts(opts, (entity) => {
|
|
60
81
|
// For now putting the `EntityChanges` cast here to avoid breaking cannotBeUpdated rules
|
|
61
82
|
// on base types, see Publisher.ts's `cannotBeUpdated("type")` repro.
|
|
62
83
|
if (entity.changes[field].hasUpdated) {
|
|
63
|
-
return
|
|
64
|
-
if (!result) {
|
|
65
|
-
return { field, code: ValidationCode.cannotBeUpdated, message: `${field} cannot be updated` };
|
|
66
|
-
}
|
|
67
|
-
return undefined;
|
|
68
|
-
});
|
|
84
|
+
return { field, code: ValidationCode.cannotBeUpdated, message: `${field} cannot be updated` };
|
|
69
85
|
}
|
|
70
|
-
return
|
|
71
|
-
};
|
|
72
|
-
return Object.assign(fn, { field, immutable: unless === undefined });
|
|
86
|
+
return;
|
|
87
|
+
});
|
|
88
|
+
return Object.assign(fn, { field, immutable: opts.unless === undefined && opts.if === undefined });
|
|
73
89
|
}
|
|
74
90
|
function isCannotBeUpdatedRule(rule) {
|
|
75
91
|
return "field" in rule && "immutable" in rule;
|
|
76
92
|
}
|
|
77
93
|
/** For STI, enforces subtype-specific relations/FKs at runtime. */
|
|
78
|
-
function mustBeSubType(relationName) {
|
|
79
|
-
return (entity) => {
|
|
94
|
+
function mustBeSubType(relationName, opts = {}) {
|
|
95
|
+
return ruleWithOpts(opts, (entity) => {
|
|
80
96
|
const m2o = entity[relationName];
|
|
81
97
|
const other = m2o.get;
|
|
82
98
|
const otherCstr = m2o.otherMeta.cstr;
|
|
83
99
|
if (other && !(other instanceof otherCstr)) {
|
|
84
100
|
return `${relationName} must be a ${otherCstr.name} not ${other}`;
|
|
85
101
|
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
function errorMessage(errors) {
|
|
89
|
-
if (errors.length === 1) {
|
|
90
|
-
return `Validation error: ${errors[0].entity.toString()} ${errors[0].message}`;
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
const message = [...(0, utils_1.groupBy)(errors, (e) => e.entity.toString()).entries()]
|
|
94
|
-
.map(([entityToString, errors]) => `${entityToString} ${errors.map((e) => e.message).join(", ")}`)
|
|
95
|
-
.join(", ");
|
|
96
|
-
return `Validation errors (${errors.length}): ${message}`;
|
|
97
|
-
}
|
|
102
|
+
});
|
|
98
103
|
}
|
|
99
104
|
/**
|
|
100
105
|
* Creates a validation rule that a field value cannot be smaller than a
|
|
@@ -102,15 +107,18 @@ function errorMessage(errors) {
|
|
|
102
107
|
*
|
|
103
108
|
* @param field The field to validate
|
|
104
109
|
* @param minValue The inclusive minimum value. e.g 0 range is [0, Infinity]
|
|
110
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
111
|
+
* - unless: Function that returns true to skip validation
|
|
112
|
+
* - if: Function that returns true to run validation
|
|
105
113
|
*
|
|
106
114
|
* @example
|
|
107
115
|
* // Age cannot be smaller than 0
|
|
108
116
|
* config.addRule(minValueRule("age", 0));
|
|
109
117
|
*/
|
|
110
|
-
function minValueRule(field, minValue) {
|
|
111
|
-
return (entity) => {
|
|
118
|
+
function minValueRule(field, minValue, opts = {}) {
|
|
119
|
+
return ruleWithOpts(opts, (entity) => {
|
|
112
120
|
const value = entity[field];
|
|
113
|
-
// Ignore undefined and null values
|
|
121
|
+
// Ignore undefined and null values. These should be handled by required rules
|
|
114
122
|
if (value === undefined || value === null)
|
|
115
123
|
return;
|
|
116
124
|
// Show an error when the value type is not a number
|
|
@@ -126,7 +134,7 @@ function minValueRule(field, minValue) {
|
|
|
126
134
|
};
|
|
127
135
|
}
|
|
128
136
|
return;
|
|
129
|
-
};
|
|
137
|
+
});
|
|
130
138
|
}
|
|
131
139
|
/**
|
|
132
140
|
* Creates a validation rule that a field value cannot be greater than a
|
|
@@ -134,15 +142,18 @@ function minValueRule(field, minValue) {
|
|
|
134
142
|
*
|
|
135
143
|
* @param field The field to validate
|
|
136
144
|
* @param maxValue The include maximum value. e.g 10 range is [-Infinity, 10]
|
|
145
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
146
|
+
* - unless: Function that returns true to skip validation
|
|
147
|
+
* - if: Function that returns true to run validation
|
|
137
148
|
*
|
|
138
149
|
* @example
|
|
139
150
|
* // Age cannot be greater than 150
|
|
140
151
|
* config.addRule(maxValueRule("age", 150));
|
|
141
152
|
*/
|
|
142
|
-
function maxValueRule(field, maxValue) {
|
|
143
|
-
return (entity) => {
|
|
153
|
+
function maxValueRule(field, maxValue, opts = {}) {
|
|
154
|
+
return ruleWithOpts(opts, (entity) => {
|
|
144
155
|
const value = entity[field];
|
|
145
|
-
// Ignore undefined and null values
|
|
156
|
+
// Ignore undefined and null values. These should be handled by required rules
|
|
146
157
|
if (value === undefined || value === null)
|
|
147
158
|
return;
|
|
148
159
|
// Show an error when the value type is not a number
|
|
@@ -158,7 +169,7 @@ function maxValueRule(field, maxValue) {
|
|
|
158
169
|
};
|
|
159
170
|
}
|
|
160
171
|
return;
|
|
161
|
-
};
|
|
172
|
+
});
|
|
162
173
|
}
|
|
163
174
|
/**
|
|
164
175
|
* Creates a validation rule that a field value must be within a range of
|
|
@@ -167,14 +178,16 @@ function maxValueRule(field, maxValue) {
|
|
|
167
178
|
* @param field The field to validate
|
|
168
179
|
* @param minValue The inclusive minimum value. e.g 0 [0, Infinity]
|
|
169
180
|
* @param maxValue The include maximum value. e.g 10 [-Infinity, 10]
|
|
181
|
+
* @param opts Optional validation rule options that control when the rule should run:
|
|
182
|
+
* - unless: Function that returns true to skip validation
|
|
183
|
+
* - if: Function that returns true to run validation * @example
|
|
170
184
|
*
|
|
171
|
-
* @example
|
|
172
185
|
* // Age must be between 0 and 150
|
|
173
186
|
* config.addRule(rangeValueRule("age", 0, 150));
|
|
174
187
|
*/
|
|
175
|
-
function rangeValueRule(field, minValue, maxValue) {
|
|
176
|
-
return (entity) => {
|
|
177
|
-
// Check min and max value rules
|
|
188
|
+
function rangeValueRule(field, minValue, maxValue, opts = {}) {
|
|
189
|
+
return ruleWithOpts(opts, (entity) => {
|
|
190
|
+
// Check min and max value rules. Because these are called without opts, it should be safe to treat them as sync.
|
|
178
191
|
const minValueResult = minValueRule(field, minValue)(entity);
|
|
179
192
|
const maxValueResult = maxValueRule(field, maxValue)(entity);
|
|
180
193
|
// Return any errors or nothing
|
|
@@ -183,6 +196,6 @@ function rangeValueRule(field, minValue, maxValue) {
|
|
|
183
196
|
if (maxValueResult)
|
|
184
197
|
return maxValueResult;
|
|
185
198
|
return;
|
|
186
|
-
};
|
|
199
|
+
});
|
|
187
200
|
}
|
|
188
201
|
//# sourceMappingURL=rules.js.map
|
package/build/rules.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":";;;AAgGA,0CAkBC;AAeD,0CAcC;AAID,sDAEC;AAGD,sCAYC;AAgBD,oCAuBC;AAgBD,oC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AAyB,CAAC;QAC3F,+BAA+B;QAC/B,IAAI,cAAc;YAAE,OAAO,cAAc,CAAC;QAC1C,IAAI,cAAc;YAAE,OAAO,cAAc,CAAC;QAC1C,OAAO;IACT,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-orm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.260.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"@types/pg": "^8.15.4",
|
|
25
25
|
"ansis": "^3.17.0",
|
|
26
26
|
"dataloader": "^2.2.3",
|
|
27
|
-
"joist-utils": "1.
|
|
27
|
+
"joist-utils": "1.260.0",
|
|
28
28
|
"knex": "^3.1.0",
|
|
29
29
|
"object-hash": "^3.0.0",
|
|
30
30
|
"pg": "^8.16.2",
|