@azure-net/kit 1.2.4 → 1.2.5

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.
@@ -19,7 +19,7 @@ export const createAsyncHelpers = (opts) => {
19
19
  };
20
20
  }
21
21
  try {
22
- const response = await Promise.resolve(typeof action === 'function' ? action() : action);
22
+ const response = await Promise.resolve(typeof action === 'function' ? (action)() : action);
23
23
  const result = { response, success: true };
24
24
  await args?.onSuccess?.(result);
25
25
  return result;
@@ -41,7 +41,7 @@ export const createAsyncHelpers = (opts) => {
41
41
  return args.fallbackResponse;
42
42
  }
43
43
  try {
44
- const response = await Promise.resolve(typeof action === 'function' ? action() : action);
44
+ const response = await Promise.resolve(typeof action === 'function' ? (action)() : action);
45
45
  await args?.onSuccess?.(response);
46
46
  return response;
47
47
  }
@@ -31,13 +31,18 @@ interface SchemaBuilder<SchemaData, Rules = unknown, TransformResult = SchemaDat
31
31
  with<M extends Record<string, unknown>>(callback: () => M): SchemaBuilder<SchemaData, Rules, TransformResult, M>;
32
32
  create(): Schema<SchemaData, TransformResult, CustomMethods>;
33
33
  }
34
- interface SchemaInstance<TransformResult> {
35
- json(): TransformResult;
36
- formData(): FormData;
37
- validated(): boolean;
34
+ interface SchemaInstance<TransformResult, SchemaData> {
35
+ json(validate?: boolean): TransformResult;
36
+ formData(validate?: boolean): FormData;
37
+ validated(): {
38
+ valid: boolean;
39
+ errors: RequestErrors<SchemaData>;
40
+ json(): TransformResult;
41
+ formData(): FormData;
42
+ };
38
43
  }
39
44
  export type Schema<SchemaData, TransformResult, CustomMethods> = CustomMethods & {
40
- from(data: Partial<SchemaData> | FormData): SchemaInstance<TransformResult>;
45
+ from(data: Partial<SchemaData> | FormData): SchemaInstance<TransformResult, SchemaData>;
41
46
  getSchemaError(e: unknown): RequestErrors<SchemaData> | undefined;
42
47
  };
43
48
  export declare const createSchemaFactory: <Rules extends Record<string, unknown>>(rules: Rules) => <SchemaData>() => SchemaBuilder<SchemaData, Rules>;
@@ -70,38 +70,44 @@ class SchemaBuilderImpl {
70
70
  throw Error('Data to validate is not an object');
71
71
  }
72
72
  const validated = () => {
73
- if (!rules)
74
- return true;
75
73
  _isValid = true;
76
74
  _errors = {};
77
- const definedSchema = rules(rulesFactory);
78
- for (const key in definedSchema) {
79
- const fieldRules = definedSchema[key] ?? [];
80
- const value = getByPath(key, _preparedData);
81
- for (const rule of fieldRules) {
82
- const failMessage = rule({
83
- val: value,
84
- listValues: _preparedData,
85
- key: key
86
- });
87
- if (failMessage) {
88
- setByPath(_errors, key, failMessage);
89
- _isValid = false;
90
- break;
75
+ const definedSchema = rules?.(rulesFactory);
76
+ if (definedSchema && typeof definedSchema === 'object' && Object.keys(definedSchema).length) {
77
+ for (const key in definedSchema) {
78
+ const fieldRules = definedSchema[key] ?? [];
79
+ const value = getByPath(key, _preparedData);
80
+ for (const rule of fieldRules) {
81
+ const failMessage = rule({
82
+ val: value,
83
+ listValues: _preparedData,
84
+ key: key
85
+ });
86
+ if (failMessage) {
87
+ setByPath(_errors, key, failMessage);
88
+ _isValid = false;
89
+ break;
90
+ }
91
91
  }
92
92
  }
93
93
  }
94
- if (!_isValid) {
94
+ return {
95
+ valid: _isValid,
96
+ errors: _errors,
97
+ json: () => json(false),
98
+ formData: () => formData(false)
99
+ };
100
+ };
101
+ const json = (validate = true) => {
102
+ if (validate && !validated().valid) {
95
103
  throw new SchemaFail(_errors);
96
104
  }
97
- return true;
98
- };
99
- const json = () => {
100
- validated();
101
105
  return transform(_preparedData);
102
106
  };
103
- const formData = () => {
104
- validated();
107
+ const formData = (validate = true) => {
108
+ if (validate && !validated().valid) {
109
+ throw new SchemaFail(_errors);
110
+ }
105
111
  return FormDataUtil.fromObject(transform(_preparedData));
106
112
  };
107
113
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",