rastack 0.0.16 → 0.0.19

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.
Files changed (105) hide show
  1. package/CHANGELOG.md +43874 -43013
  2. package/dist/.prettierrc +7 -7
  3. package/dist/compile/analyze.js +19 -11
  4. package/dist/compile/index.js +26 -17
  5. package/dist/compile/manifest.js +2 -3
  6. package/dist/compile/openapi.js +5 -8
  7. package/dist/compile/program.js +19 -10
  8. package/dist/csv-schema.js +5 -6
  9. package/dist/define/index.js +2 -2
  10. package/dist/entity-generation/delete-method.js +8 -10
  11. package/dist/entity-generation/form.js +80 -82
  12. package/dist/entity-generation/get-method.js +35 -36
  13. package/dist/entity-generation/imports.js +24 -20
  14. package/dist/entity-generation/sync-method.d.ts +1 -1
  15. package/dist/entity-generation/sync-method.js +45 -46
  16. package/dist/entity-generation/update-method.js +9 -10
  17. package/dist/rad-wasm-build.d.ts +1 -1
  18. package/dist/rad-wasm-build.js +15 -6
  19. package/dist/rad.js +9 -2
  20. package/dist/schema/camel-to-pastel.js +1 -2
  21. package/dist/schema/capitalise-first-letter.js +1 -2
  22. package/dist/schema/extract-response.js +3 -4
  23. package/dist/schema/fetch-schema.js +4 -16
  24. package/dist/schema/remove-non-model-paths.js +1 -2
  25. package/dist/schema/to-camel-case.js +1 -2
  26. package/dist/schema/to-pastel-case.js +1 -2
  27. package/dist/schema-convert.js +22 -33
  28. package/dist/schema-entities.js +177 -189
  29. package/dist/schema-fetch.js +46 -63
  30. package/dist/schema-full.js +23 -36
  31. package/dist/schema-index.js +16 -27
  32. package/dist/schema-params.js +11 -22
  33. package/dist/seed.js +95 -97
  34. package/hooks/form/form.ts +207 -207
  35. package/hooks/form/index.ts +8 -8
  36. package/hooks/form/interfaces.ts +217 -217
  37. package/hooks/form/structure.ts +39 -39
  38. package/hooks/form/validate-schema.ts +49 -49
  39. package/hooks/index.ts +3 -3
  40. package/hooks/query/api.ts +42 -42
  41. package/hooks/query/delete.ts +45 -45
  42. package/hooks/query/fetch.ts +48 -48
  43. package/hooks/query/index.ts +21 -21
  44. package/hooks/query/interfaces.ts +111 -111
  45. package/hooks/query/list.ts +286 -286
  46. package/hooks/query/update.ts +88 -88
  47. package/hooks/query/url.ts +31 -31
  48. package/hooks/real-time/index.ts +1 -1
  49. package/hooks/real-time/pusher.ts +43 -43
  50. package/jest.config.cjs +6 -6
  51. package/package.json +57 -57
  52. package/provider/index.ts +13 -13
  53. package/provider/provider.tsx +187 -187
  54. package/provider/types.ts +114 -114
  55. package/provider/warehouse.ts +167 -167
  56. package/provider/wasm.ts +124 -124
  57. package/runtime.ts +3 -3
  58. package/src/.prettierrc +7 -7
  59. package/src/compile/analyze.ts +224 -224
  60. package/src/compile/index.ts +86 -86
  61. package/src/compile/manifest.ts +10 -10
  62. package/src/compile/model.ts +69 -69
  63. package/src/compile/openapi.ts +266 -266
  64. package/src/compile/program.ts +40 -40
  65. package/src/csv-schema.ts +187 -187
  66. package/src/define/index.ts +162 -162
  67. package/src/entity-generation/delete-method.ts +37 -37
  68. package/src/entity-generation/form.ts +169 -169
  69. package/src/entity-generation/get-method.ts +80 -80
  70. package/src/entity-generation/imports.ts +110 -110
  71. package/src/entity-generation/sync-method.ts +222 -222
  72. package/src/entity-generation/update-method.ts +38 -38
  73. package/src/index.ts +77 -77
  74. package/src/rad-compile.ts +88 -88
  75. package/src/rad-wasm-build.ts +102 -102
  76. package/src/rad.ts +101 -101
  77. package/src/scan.ts +89 -89
  78. package/src/schema/camel-to-pastel.ts +3 -3
  79. package/src/schema/capitalise-first-letter.ts +3 -3
  80. package/src/schema/extract-response.ts +38 -38
  81. package/src/schema/fetch-schema.ts +12 -12
  82. package/src/schema/remove-non-model-paths.ts +13 -13
  83. package/src/schema/to-camel-case.ts +7 -7
  84. package/src/schema/to-pastel-case.ts +6 -6
  85. package/src/schema-convert.ts +61 -61
  86. package/src/schema-entities.ts +401 -401
  87. package/src/schema-fetch.ts +82 -82
  88. package/src/schema-full.ts +38 -38
  89. package/src/schema-index.ts +29 -29
  90. package/src/schema-params.ts +116 -116
  91. package/src/seed.ts +297 -297
  92. package/sync/engine.ts +392 -392
  93. package/sync/hooks.ts +450 -450
  94. package/sync/index.ts +8 -8
  95. package/sync/persistence.ts +237 -237
  96. package/sync/provider.tsx +91 -91
  97. package/sync/registry.ts +36 -36
  98. package/sync/store.ts +126 -126
  99. package/sync/transactions.ts +300 -300
  100. package/sync/types.ts +94 -94
  101. package/test/compile.spec.ts +192 -192
  102. package/test/csv-schema.spec.ts +143 -143
  103. package/test/schema-entities.spec.ts +688 -688
  104. package/tsconfig.json +24 -24
  105. package/types.ts +18 -18
@@ -1,217 +1,217 @@
1
- import { FlattenedRecord } from "../../types";
2
- import { SchemaObject } from "ajv";
3
- import { IUpdateQueryResult } from "../../hooks";
4
- import { FormikValues } from "formik";
5
-
6
- export interface FormFieldStructure<TValue> {
7
- value: TValue;
8
- error: string;
9
- type: FormType;
10
- isChecked: boolean;
11
- isError: boolean;
12
- isDisabled: boolean;
13
- isRequired: boolean;
14
- setValue: (value: TValue) => void;
15
- setTouched: (isTouched: boolean) => void;
16
- }
17
-
18
- interface FormMutation<
19
- TPostParams,
20
- TPutParams,
21
- TPatchParams,
22
- TPostPayload,
23
- TPutPayload,
24
- TPatchPayload,
25
- TPostResult,
26
- TPutResult,
27
- TPatchResult,
28
- > {
29
- post: {
30
- mutation?: IUpdateQueryResult<TPostParams, TPostPayload, TPostResult>;
31
- validation?: SchemaObject | undefined;
32
- };
33
- put: {
34
- mutation?: IUpdateQueryResult<TPutParams, TPutPayload, TPutResult>;
35
- validation?: SchemaObject | undefined;
36
- };
37
- patch: {
38
- mutation?: IUpdateQueryResult<TPatchParams, TPatchPayload, TPatchResult>;
39
- validation?: SchemaObject | undefined;
40
- };
41
- }
42
-
43
- export type FormUpdate = "post_or_patch" | "put";
44
-
45
- export interface FormProps<
46
- TFormValues extends object,
47
- TPostParams,
48
- TPutParams,
49
- TPatchParams,
50
- TPostPayload,
51
- TPutPayload,
52
- TPatchPayload,
53
- TPostResult,
54
- TPutResult,
55
- TPatchResult,
56
- TFormUpdate extends FormUpdate,
57
- TSourceId extends string | undefined,
58
- > {
59
- id?: TSourceId & string;
60
- sourceData?: SourceData;
61
- formValues: TFormValues & object;
62
- formatResponse?: ForwardMapping<
63
- TFormValues,
64
- TFormUpdate extends "put"
65
- ? TPutParams & TPutResult
66
- : TSourceId extends undefined
67
- ? TPostParams & TPostResult
68
- : TPatchParams & TPatchResult
69
- >;
70
- transformRequest?: ForwardMap<
71
- TFormValues,
72
- TFormUpdate extends "put"
73
- ? TPutParams & TPutResult
74
- : TSourceId extends undefined
75
- ? TPostParams & TPostResult
76
- : TPatchParams & TPatchResult
77
- >;
78
- transformResponse?: ReverseMap<
79
- TFormValues,
80
- TFormUpdate extends "put"
81
- ? TPutParams & TPutResult
82
- : TSourceId extends undefined
83
- ? TPostParams & TPostResult
84
- : TPatchParams & TPatchResult
85
- >;
86
- updateType?: TFormUpdate;
87
- mutations?: FormMutation<
88
- TPostParams,
89
- TPutParams,
90
- TPatchParams,
91
- TPostPayload,
92
- TPutPayload,
93
- TPatchPayload,
94
- TPostResult,
95
- TPutResult,
96
- TPatchResult
97
- >;
98
- onSuccess?: (
99
- values: TFormUpdate extends "put"
100
- ? TPutResult
101
- : TSourceId extends undefined
102
- ? TPostResult
103
- : TPatchResult,
104
- ) => void;
105
- }
106
-
107
- export interface FormStructure<TFormValues, TResponse> {
108
- formValues: TFormValues | undefined;
109
- responseValues: TResponse | undefined;
110
- errors: any;
111
- onSubmit: () => void;
112
- resetForm: () => void;
113
- isSubmitDisabled: boolean;
114
- isSourceDataLoading: boolean;
115
- isSubmitting: boolean;
116
- getFormField: <TFieldName extends keyof FlattenedRecord<TFormValues>>(
117
- fieldName: TFieldName,
118
- ) => FormFieldStructure<FlattenedRecord<TFormValues>[TFieldName]>;
119
- }
120
-
121
- export type FormType = "number" | "text" | "array";
122
-
123
- export interface SetupUpdateEndpointReturn<TParams, TPayload, TResult> {
124
- mutation: IUpdateQueryResult<TParams, TPayload, TResult>;
125
- validation: SchemaObject;
126
- }
127
-
128
- export type FormUpdateMethod = "post_or_patch" | "put";
129
-
130
- export interface SourceData {
131
- [key: string]: unknown;
132
- }
133
-
134
- export type ForwardMapping<TFormValues, TResponse> = {
135
- [key in keyof TFormValues]: (value: TFormValues[key]) => {
136
- [key in keyof TResponse]: TResponse[keyof TResponse];
137
- };
138
- };
139
-
140
- export type ReverseMapping<TFormValues, TResponse> = {
141
- [key in keyof TResponse]: (value: TResponse[key]) => {
142
- [key in keyof TFormValues]: TFormValues[keyof TFormValues];
143
- };
144
- };
145
-
146
- export type ForwardMap<TFormValues, TResponse> = (value: TFormValues) => {
147
- [key in keyof TResponse]: TResponse[keyof TResponse];
148
- };
149
-
150
- export type ReverseMap<TFormValues, TResponse> = (value: TResponse) => {
151
- [key in keyof TFormValues]: TFormValues[keyof TFormValues];
152
- };
153
-
154
- type EnsureObject<T> = T extends object ? T : Record<string, never>;
155
-
156
- export type FormValuesType<
157
- TFormUpdate extends "put" | "post_or_patch",
158
- TSourceId extends string | undefined,
159
- TPutParams,
160
- TPutPayload,
161
- TPostParams,
162
- TPostPayload,
163
- TPatchParams,
164
- TPatchPayload,
165
- > = FormikValues &
166
- EnsureObject<
167
- TFormUpdate extends "put"
168
- ? TPutParams & TPutPayload
169
- : TSourceId extends undefined
170
- ? TPostParams & TPostPayload
171
- : TPatchParams & TPatchPayload
172
- >;
173
-
174
- export type FormParamsType<
175
- TFormUpdate extends "put" | "post_or_patch",
176
- TSourceId extends string | undefined,
177
- TPutParams,
178
- TPostParams,
179
- TPatchParams,
180
- > = FormikValues &
181
- EnsureObject<
182
- TFormUpdate extends "put"
183
- ? TPutParams
184
- : TSourceId extends undefined
185
- ? TPostParams
186
- : TPatchParams
187
- >;
188
-
189
- export type FormPayloadType<
190
- TFormUpdate extends "put" | "post_or_patch",
191
- TSourceId extends string | undefined,
192
- TPutPayload,
193
- TPostPayload,
194
- TPatchPayload,
195
- > = FormikValues &
196
- EnsureObject<
197
- TFormUpdate extends "put"
198
- ? TPutPayload
199
- : TSourceId extends undefined
200
- ? TPostPayload
201
- : TPatchPayload
202
- >;
203
-
204
- export type FormResultType<
205
- TFormUpdate extends "put" | "post_or_patch",
206
- TSourceId extends string | undefined,
207
- TPutResult,
208
- TPostResult,
209
- TPatchResult,
210
- > = FormikValues &
211
- EnsureObject<
212
- TFormUpdate extends "put"
213
- ? TPutResult
214
- : TSourceId extends undefined
215
- ? TPostResult
216
- : TPatchResult
217
- >;
1
+ import { FlattenedRecord } from "../../types";
2
+ import { SchemaObject } from "ajv";
3
+ import { IUpdateQueryResult } from "../../hooks";
4
+ import { FormikValues } from "formik";
5
+
6
+ export interface FormFieldStructure<TValue> {
7
+ value: TValue;
8
+ error: string;
9
+ type: FormType;
10
+ isChecked: boolean;
11
+ isError: boolean;
12
+ isDisabled: boolean;
13
+ isRequired: boolean;
14
+ setValue: (value: TValue) => void;
15
+ setTouched: (isTouched: boolean) => void;
16
+ }
17
+
18
+ interface FormMutation<
19
+ TPostParams,
20
+ TPutParams,
21
+ TPatchParams,
22
+ TPostPayload,
23
+ TPutPayload,
24
+ TPatchPayload,
25
+ TPostResult,
26
+ TPutResult,
27
+ TPatchResult,
28
+ > {
29
+ post: {
30
+ mutation?: IUpdateQueryResult<TPostParams, TPostPayload, TPostResult>;
31
+ validation?: SchemaObject | undefined;
32
+ };
33
+ put: {
34
+ mutation?: IUpdateQueryResult<TPutParams, TPutPayload, TPutResult>;
35
+ validation?: SchemaObject | undefined;
36
+ };
37
+ patch: {
38
+ mutation?: IUpdateQueryResult<TPatchParams, TPatchPayload, TPatchResult>;
39
+ validation?: SchemaObject | undefined;
40
+ };
41
+ }
42
+
43
+ export type FormUpdate = "post_or_patch" | "put";
44
+
45
+ export interface FormProps<
46
+ TFormValues extends object,
47
+ TPostParams,
48
+ TPutParams,
49
+ TPatchParams,
50
+ TPostPayload,
51
+ TPutPayload,
52
+ TPatchPayload,
53
+ TPostResult,
54
+ TPutResult,
55
+ TPatchResult,
56
+ TFormUpdate extends FormUpdate,
57
+ TSourceId extends string | undefined,
58
+ > {
59
+ id?: TSourceId & string;
60
+ sourceData?: SourceData;
61
+ formValues: TFormValues & object;
62
+ formatResponse?: ForwardMapping<
63
+ TFormValues,
64
+ TFormUpdate extends "put"
65
+ ? TPutParams & TPutResult
66
+ : TSourceId extends undefined
67
+ ? TPostParams & TPostResult
68
+ : TPatchParams & TPatchResult
69
+ >;
70
+ transformRequest?: ForwardMap<
71
+ TFormValues,
72
+ TFormUpdate extends "put"
73
+ ? TPutParams & TPutResult
74
+ : TSourceId extends undefined
75
+ ? TPostParams & TPostResult
76
+ : TPatchParams & TPatchResult
77
+ >;
78
+ transformResponse?: ReverseMap<
79
+ TFormValues,
80
+ TFormUpdate extends "put"
81
+ ? TPutParams & TPutResult
82
+ : TSourceId extends undefined
83
+ ? TPostParams & TPostResult
84
+ : TPatchParams & TPatchResult
85
+ >;
86
+ updateType?: TFormUpdate;
87
+ mutations?: FormMutation<
88
+ TPostParams,
89
+ TPutParams,
90
+ TPatchParams,
91
+ TPostPayload,
92
+ TPutPayload,
93
+ TPatchPayload,
94
+ TPostResult,
95
+ TPutResult,
96
+ TPatchResult
97
+ >;
98
+ onSuccess?: (
99
+ values: TFormUpdate extends "put"
100
+ ? TPutResult
101
+ : TSourceId extends undefined
102
+ ? TPostResult
103
+ : TPatchResult,
104
+ ) => void;
105
+ }
106
+
107
+ export interface FormStructure<TFormValues, TResponse> {
108
+ formValues: TFormValues | undefined;
109
+ responseValues: TResponse | undefined;
110
+ errors: any;
111
+ onSubmit: () => void;
112
+ resetForm: () => void;
113
+ isSubmitDisabled: boolean;
114
+ isSourceDataLoading: boolean;
115
+ isSubmitting: boolean;
116
+ getFormField: <TFieldName extends keyof FlattenedRecord<TFormValues>>(
117
+ fieldName: TFieldName,
118
+ ) => FormFieldStructure<FlattenedRecord<TFormValues>[TFieldName]>;
119
+ }
120
+
121
+ export type FormType = "number" | "text" | "array";
122
+
123
+ export interface SetupUpdateEndpointReturn<TParams, TPayload, TResult> {
124
+ mutation: IUpdateQueryResult<TParams, TPayload, TResult>;
125
+ validation: SchemaObject;
126
+ }
127
+
128
+ export type FormUpdateMethod = "post_or_patch" | "put";
129
+
130
+ export interface SourceData {
131
+ [key: string]: unknown;
132
+ }
133
+
134
+ export type ForwardMapping<TFormValues, TResponse> = {
135
+ [key in keyof TFormValues]: (value: TFormValues[key]) => {
136
+ [key in keyof TResponse]: TResponse[keyof TResponse];
137
+ };
138
+ };
139
+
140
+ export type ReverseMapping<TFormValues, TResponse> = {
141
+ [key in keyof TResponse]: (value: TResponse[key]) => {
142
+ [key in keyof TFormValues]: TFormValues[keyof TFormValues];
143
+ };
144
+ };
145
+
146
+ export type ForwardMap<TFormValues, TResponse> = (value: TFormValues) => {
147
+ [key in keyof TResponse]: TResponse[keyof TResponse];
148
+ };
149
+
150
+ export type ReverseMap<TFormValues, TResponse> = (value: TResponse) => {
151
+ [key in keyof TFormValues]: TFormValues[keyof TFormValues];
152
+ };
153
+
154
+ type EnsureObject<T> = T extends object ? T : Record<string, never>;
155
+
156
+ export type FormValuesType<
157
+ TFormUpdate extends "put" | "post_or_patch",
158
+ TSourceId extends string | undefined,
159
+ TPutParams,
160
+ TPutPayload,
161
+ TPostParams,
162
+ TPostPayload,
163
+ TPatchParams,
164
+ TPatchPayload,
165
+ > = FormikValues &
166
+ EnsureObject<
167
+ TFormUpdate extends "put"
168
+ ? TPutParams & TPutPayload
169
+ : TSourceId extends undefined
170
+ ? TPostParams & TPostPayload
171
+ : TPatchParams & TPatchPayload
172
+ >;
173
+
174
+ export type FormParamsType<
175
+ TFormUpdate extends "put" | "post_or_patch",
176
+ TSourceId extends string | undefined,
177
+ TPutParams,
178
+ TPostParams,
179
+ TPatchParams,
180
+ > = FormikValues &
181
+ EnsureObject<
182
+ TFormUpdate extends "put"
183
+ ? TPutParams
184
+ : TSourceId extends undefined
185
+ ? TPostParams
186
+ : TPatchParams
187
+ >;
188
+
189
+ export type FormPayloadType<
190
+ TFormUpdate extends "put" | "post_or_patch",
191
+ TSourceId extends string | undefined,
192
+ TPutPayload,
193
+ TPostPayload,
194
+ TPatchPayload,
195
+ > = FormikValues &
196
+ EnsureObject<
197
+ TFormUpdate extends "put"
198
+ ? TPutPayload
199
+ : TSourceId extends undefined
200
+ ? TPostPayload
201
+ : TPatchPayload
202
+ >;
203
+
204
+ export type FormResultType<
205
+ TFormUpdate extends "put" | "post_or_patch",
206
+ TSourceId extends string | undefined,
207
+ TPutResult,
208
+ TPostResult,
209
+ TPatchResult,
210
+ > = FormikValues &
211
+ EnsureObject<
212
+ TFormUpdate extends "put"
213
+ ? TPutResult
214
+ : TSourceId extends undefined
215
+ ? TPostResult
216
+ : TPatchResult
217
+ >;
@@ -1,39 +1,39 @@
1
- import { SchemaObject } from "ajv";
2
- import { FormType } from "./interfaces";
3
-
4
- interface GetFieldStructureResponse {
5
- isRequired: boolean;
6
- type: FormType;
7
- }
8
-
9
- export function getFieldStructure<TFieldName>(
10
- validationSchema: SchemaObject | undefined,
11
- fieldName: TFieldName,
12
- ): GetFieldStructureResponse {
13
- let type: FormType = "text";
14
- if (validationSchema?.properties) {
15
- const schemaJson = validationSchema.properties[fieldName];
16
- if (!schemaJson) {
17
- return { isRequired: false, type };
18
- }
19
- let schemaJsonType = schemaJson.type;
20
-
21
- if (typeof schemaJsonType === "string") {
22
- schemaJsonType = [schemaJsonType];
23
- }
24
- if (schemaJsonType) {
25
- if (schemaJsonType.includes("integer")) {
26
- type = "number";
27
- }
28
- if (schemaJsonType.includes("array")) {
29
- type = "array";
30
- }
31
- }
32
- }
33
-
34
- let isRequired = false;
35
- if (validationSchema?.required) {
36
- isRequired = validationSchema.required?.includes(fieldName);
37
- }
38
- return { isRequired, type };
39
- }
1
+ import { SchemaObject } from "ajv";
2
+ import { FormType } from "./interfaces";
3
+
4
+ interface GetFieldStructureResponse {
5
+ isRequired: boolean;
6
+ type: FormType;
7
+ }
8
+
9
+ export function getFieldStructure<TFieldName>(
10
+ validationSchema: SchemaObject | undefined,
11
+ fieldName: TFieldName,
12
+ ): GetFieldStructureResponse {
13
+ let type: FormType = "text";
14
+ if (validationSchema?.properties) {
15
+ const schemaJson = validationSchema.properties[fieldName];
16
+ if (!schemaJson) {
17
+ return { isRequired: false, type };
18
+ }
19
+ let schemaJsonType = schemaJson.type;
20
+
21
+ if (typeof schemaJsonType === "string") {
22
+ schemaJsonType = [schemaJsonType];
23
+ }
24
+ if (schemaJsonType) {
25
+ if (schemaJsonType.includes("integer")) {
26
+ type = "number";
27
+ }
28
+ if (schemaJsonType.includes("array")) {
29
+ type = "array";
30
+ }
31
+ }
32
+ }
33
+
34
+ let isRequired = false;
35
+ if (validationSchema?.required) {
36
+ isRequired = validationSchema.required?.includes(fieldName);
37
+ }
38
+ return { isRequired, type };
39
+ }
@@ -1,49 +1,49 @@
1
- import { FormikErrors } from "formik";
2
- import addFormats from "ajv-formats";
3
- import Ajv, { ErrorObject } from "ajv";
4
-
5
- export function createAJV() {
6
- const ajvInstance = new Ajv({ allErrors: true });
7
- addFormats(ajvInstance);
8
-
9
- ajvInstance.addFormat("decimal", {
10
- type: "number",
11
- validate: (num: number) =>
12
- // @ts-ignore
13
- !Number.isNaN(num) && num.toString().includes("."),
14
- });
15
-
16
- return ajvInstance;
17
- }
18
-
19
- export function formikJSONSchemaValidation<TData>(
20
- data: TData,
21
- schema?: any | undefined,
22
- ): FormikErrors<TData> {
23
- const AJV = createAJV();
24
- const validate = AJV.compile(schema);
25
- const valid = validate(data);
26
- if (valid) return {};
27
-
28
- const errors: FormikErrors<TData> = {};
29
- validate.errors?.forEach((error) => {
30
- const ajvError = error as ErrorObject & {
31
- dataPath: string;
32
- params: { missingProperty?: string };
33
- };
34
- let path = ajvError.instancePath.replace(/^\//, "").replace(/\//g, ".");
35
-
36
- if (
37
- // @ts-ignore
38
- ajvError.schemaPath.includes("#/required") &&
39
- ajvError.params.missingProperty
40
- ) {
41
- path = `${path}.${ajvError.params.missingProperty}`.replace(/^\./, "");
42
- (errors as Record<string, string>)[path] =
43
- `${ajvError.params.missingProperty} is required`;
44
- } else {
45
- (errors as Record<string, string>)[path] = ajvError.message as string;
46
- }
47
- });
48
- return errors;
49
- }
1
+ import { FormikErrors } from "formik";
2
+ import addFormats from "ajv-formats";
3
+ import Ajv, { ErrorObject } from "ajv";
4
+
5
+ export function createAJV() {
6
+ const ajvInstance = new Ajv({ allErrors: true });
7
+ addFormats(ajvInstance);
8
+
9
+ ajvInstance.addFormat("decimal", {
10
+ type: "number",
11
+ validate: (num: number) =>
12
+ // @ts-ignore
13
+ !Number.isNaN(num) && num.toString().includes("."),
14
+ });
15
+
16
+ return ajvInstance;
17
+ }
18
+
19
+ export function formikJSONSchemaValidation<TData>(
20
+ data: TData,
21
+ schema?: any | undefined,
22
+ ): FormikErrors<TData> {
23
+ const AJV = createAJV();
24
+ const validate = AJV.compile(schema);
25
+ const valid = validate(data);
26
+ if (valid) return {};
27
+
28
+ const errors: FormikErrors<TData> = {};
29
+ validate.errors?.forEach((error) => {
30
+ const ajvError = error as ErrorObject & {
31
+ dataPath: string;
32
+ params: { missingProperty?: string };
33
+ };
34
+ let path = ajvError.instancePath.replace(/^\//, "").replace(/\//g, ".");
35
+
36
+ if (
37
+ // @ts-ignore
38
+ ajvError.schemaPath.includes("#/required") &&
39
+ ajvError.params.missingProperty
40
+ ) {
41
+ path = `${path}.${ajvError.params.missingProperty}`.replace(/^\./, "");
42
+ (errors as Record<string, string>)[path] =
43
+ `${ajvError.params.missingProperty} is required`;
44
+ } else {
45
+ (errors as Record<string, string>)[path] = ajvError.message as string;
46
+ }
47
+ });
48
+ return errors;
49
+ }
package/hooks/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./form";
2
- export * from "./query";
3
- export * from "./real-time";
1
+ export * from "./form";
2
+ export * from "./query";
3
+ export * from "./real-time";