@azure-net/kit 3.0.53 → 3.0.54
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.
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
type TransformationParams<SchemaData> = {
|
|
2
|
+
validate?: boolean;
|
|
3
|
+
onValidationError?: (errors: RequestErrors<SchemaData>) => void;
|
|
4
|
+
};
|
|
1
5
|
type DeepKeys<SchemaData> = SchemaData extends object ? {
|
|
2
6
|
[K in keyof SchemaData & string]: SchemaData[K] extends object ? K | `${K}.${DeepKeys<SchemaData[K]>}` : K;
|
|
3
7
|
}[keyof SchemaData & string] : never;
|
|
@@ -32,8 +36,8 @@ interface SchemaBuilder<SchemaData, Rules = unknown, TransformResult = SchemaDat
|
|
|
32
36
|
create(): Schema<SchemaData, TransformResult, CustomMethods>;
|
|
33
37
|
}
|
|
34
38
|
interface SchemaInstance<TransformResult, SchemaData> {
|
|
35
|
-
json(
|
|
36
|
-
formData(
|
|
39
|
+
json(params?: TransformationParams<SchemaData>): TransformResult;
|
|
40
|
+
formData(params?: TransformationParams<SchemaData>): FormData;
|
|
37
41
|
validated(): {
|
|
38
42
|
valid: boolean;
|
|
39
43
|
errors: RequestErrors<SchemaData>;
|
|
@@ -69,7 +69,7 @@ class SchemaBuilderImpl {
|
|
|
69
69
|
if (typeof _preparedData !== 'object') {
|
|
70
70
|
throw Error('Data to validate is not an object');
|
|
71
71
|
}
|
|
72
|
-
const validated = () => {
|
|
72
|
+
const validated = (params) => {
|
|
73
73
|
_isValid = true;
|
|
74
74
|
_errors = {};
|
|
75
75
|
const definedSchema = rules?.(rulesFactory);
|
|
@@ -91,21 +91,26 @@ class SchemaBuilderImpl {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
+
if (!_isValid && params?.onValidationError) {
|
|
95
|
+
params.onValidationError(_errors);
|
|
96
|
+
}
|
|
94
97
|
return {
|
|
95
98
|
valid: _isValid,
|
|
96
99
|
errors: _errors,
|
|
97
|
-
json: () => json(false),
|
|
98
|
-
formData: () => formData(false)
|
|
100
|
+
json: () => json({ validate: false }),
|
|
101
|
+
formData: () => formData({ validate: false })
|
|
99
102
|
};
|
|
100
103
|
};
|
|
101
|
-
const json = (
|
|
102
|
-
|
|
104
|
+
const json = (params = {}) => {
|
|
105
|
+
const { validate = true, onValidationError } = params;
|
|
106
|
+
if (validate && !validated({ onValidationError }).valid) {
|
|
103
107
|
throw new SchemaFail(_errors);
|
|
104
108
|
}
|
|
105
109
|
return transform(_preparedData);
|
|
106
110
|
};
|
|
107
|
-
const formData = (
|
|
108
|
-
|
|
111
|
+
const formData = (params = {}) => {
|
|
112
|
+
const { validate = true, onValidationError } = params;
|
|
113
|
+
if (validate && !validated({ onValidationError }).valid) {
|
|
109
114
|
throw new SchemaFail(_errors);
|
|
110
115
|
}
|
|
111
116
|
return FormDataUtil.fromObject(transform(_preparedData));
|