remix-validated-form 2.0.0 → 2.1.1

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 (107) hide show
  1. package/.turbo/turbo-build.log +9 -0
  2. package/.turbo/turbo-dev.log +0 -0
  3. package/.turbo/turbo-test.log +36 -0
  4. package/README.md +3 -6
  5. package/browser/ValidatedForm.d.ts +13 -1
  6. package/browser/ValidatedForm.js +72 -19
  7. package/browser/hooks.d.ts +0 -0
  8. package/browser/hooks.js +0 -0
  9. package/browser/index.d.ts +0 -0
  10. package/browser/index.js +0 -0
  11. package/browser/internal/flatten.d.ts +0 -0
  12. package/browser/internal/flatten.js +0 -0
  13. package/browser/internal/formContext.d.ts +0 -0
  14. package/browser/internal/formContext.js +0 -0
  15. package/browser/internal/submissionCallbacks.d.ts +1 -0
  16. package/browser/internal/submissionCallbacks.js +13 -0
  17. package/browser/internal/util.d.ts +0 -0
  18. package/browser/internal/util.js +0 -0
  19. package/browser/server.d.ts +0 -0
  20. package/browser/server.js +0 -0
  21. package/browser/test-data/testFormData.d.ts +0 -0
  22. package/browser/test-data/testFormData.js +0 -0
  23. package/browser/validation/createValidator.d.ts +0 -0
  24. package/browser/validation/createValidator.js +12 -1
  25. package/browser/validation/types.d.ts +3 -0
  26. package/browser/validation/types.js +0 -0
  27. package/browser/validation/validation.test.d.ts +0 -0
  28. package/browser/validation/validation.test.js +5 -0
  29. package/browser/validation/withYup.d.ts +0 -0
  30. package/browser/validation/withYup.js +0 -0
  31. package/browser/validation/withZod.d.ts +0 -0
  32. package/browser/validation/withZod.js +0 -0
  33. package/build/ValidatedForm.d.ts +13 -1
  34. package/build/ValidatedForm.js +71 -18
  35. package/build/hooks.d.ts +0 -0
  36. package/build/hooks.js +0 -0
  37. package/build/index.d.ts +0 -0
  38. package/build/index.js +0 -0
  39. package/build/internal/flatten.d.ts +0 -0
  40. package/build/internal/flatten.js +0 -0
  41. package/build/internal/formContext.d.ts +0 -0
  42. package/build/internal/formContext.js +0 -0
  43. package/build/internal/submissionCallbacks.d.ts +1 -0
  44. package/build/internal/submissionCallbacks.js +17 -0
  45. package/build/internal/util.d.ts +0 -0
  46. package/build/internal/util.js +0 -0
  47. package/build/server.d.ts +0 -0
  48. package/build/server.js +0 -0
  49. package/build/test-data/testFormData.d.ts +0 -0
  50. package/build/test-data/testFormData.js +0 -0
  51. package/build/validation/createValidator.d.ts +0 -0
  52. package/build/validation/createValidator.js +12 -1
  53. package/build/validation/types.d.ts +3 -0
  54. package/build/validation/types.js +0 -0
  55. package/build/validation/validation.test.d.ts +0 -0
  56. package/build/validation/validation.test.js +5 -0
  57. package/build/validation/withYup.d.ts +0 -0
  58. package/build/validation/withYup.js +0 -0
  59. package/build/validation/withZod.d.ts +0 -0
  60. package/build/validation/withZod.js +0 -0
  61. package/jest.config.js +5 -0
  62. package/package.json +8 -33
  63. package/src/ValidatedForm.tsx +229 -0
  64. package/src/hooks.ts +60 -0
  65. package/src/index.ts +8 -0
  66. package/src/internal/flatten.ts +48 -0
  67. package/src/internal/formContext.ts +36 -0
  68. package/src/internal/submissionCallbacks.ts +15 -0
  69. package/src/internal/util.ts +23 -0
  70. package/src/server.ts +10 -0
  71. package/src/test-data/testFormData.ts +55 -0
  72. package/src/validation/createValidator.ts +34 -0
  73. package/src/validation/types.ts +28 -0
  74. package/src/validation/validation.test.ts +322 -0
  75. package/src/validation/withYup.ts +43 -0
  76. package/src/validation/withZod.ts +51 -0
  77. package/tsconfig.json +5 -0
  78. package/.eslintcache +0 -1
  79. package/.eslintignore +0 -1
  80. package/.prettierignore +0 -10
  81. package/LICENSE +0 -21
  82. package/browser/flatten.d.ts +0 -4
  83. package/browser/flatten.js +0 -35
  84. package/build/flatten.d.ts +0 -4
  85. package/build/flatten.js +0 -43
  86. package/sample-app/.env +0 -7
  87. package/sample-app/README.md +0 -53
  88. package/sample-app/app/components/ErrorBox.tsx +0 -34
  89. package/sample-app/app/components/FormInput.tsx +0 -40
  90. package/sample-app/app/components/FormSelect.tsx +0 -37
  91. package/sample-app/app/components/SubjectForm.tsx +0 -150
  92. package/sample-app/app/entry.client.tsx +0 -4
  93. package/sample-app/app/entry.server.tsx +0 -21
  94. package/sample-app/app/root.tsx +0 -92
  95. package/sample-app/app/routes/index.tsx +0 -5
  96. package/sample-app/app/routes/subjects/$id.edit.tsx +0 -98
  97. package/sample-app/app/routes/subjects/index.tsx +0 -112
  98. package/sample-app/app/routes/subjects/new.tsx +0 -46
  99. package/sample-app/app/services/db.server.ts +0 -23
  100. package/sample-app/app/types.ts +0 -6
  101. package/sample-app/package-lock.json +0 -10890
  102. package/sample-app/package.json +0 -36
  103. package/sample-app/prisma/dev.db +0 -0
  104. package/sample-app/prisma/schema.prisma +0 -34
  105. package/sample-app/public/favicon.ico +0 -0
  106. package/sample-app/remix.config.js +0 -10
  107. package/sample-app/remix.env.d.ts +0 -2
@@ -0,0 +1,322 @@
1
+ import * as yup from "yup";
2
+ import { z } from "zod";
3
+ import { Validator, withYup } from "..";
4
+ import { objectFromPathEntries } from "../internal/flatten";
5
+ import { TestFormData } from "../test-data/testFormData";
6
+ import { withZod } from "./withZod";
7
+
8
+ // If adding an adapter, write a validator that validates this shape
9
+ type Person = {
10
+ firstName: string;
11
+ lastName: string;
12
+ age?: number;
13
+ address: {
14
+ streetAddress: string;
15
+ city: string;
16
+ country: string;
17
+ };
18
+ pets?: {
19
+ animal: string;
20
+ name: string;
21
+ }[];
22
+ };
23
+
24
+ type ValidationTestCase = {
25
+ name: string;
26
+ validator: Validator<Person>;
27
+ };
28
+
29
+ const validationTestCases: ValidationTestCase[] = [
30
+ {
31
+ name: "yup",
32
+ validator: withYup(
33
+ yup.object({
34
+ firstName: yup.string().required(),
35
+ lastName: yup.string().required(),
36
+ age: yup.number(),
37
+ address: yup
38
+ .object({
39
+ streetAddress: yup.string().required(),
40
+ city: yup.string().required(),
41
+ country: yup.string().required(),
42
+ })
43
+ .required(),
44
+ pets: yup.array().of(
45
+ yup.object({
46
+ animal: yup.string().required(),
47
+ name: yup.string().required(),
48
+ })
49
+ ),
50
+ })
51
+ ),
52
+ },
53
+ {
54
+ name: "zod",
55
+ validator: withZod(
56
+ z.object({
57
+ firstName: z.string().nonempty(),
58
+ lastName: z.string().nonempty(),
59
+ age: z.optional(z.number()),
60
+ address: z.preprocess(
61
+ (value) => (value == null ? {} : value),
62
+ z.object({
63
+ streetAddress: z.string().nonempty(),
64
+ city: z.string().nonempty(),
65
+ country: z.string().nonempty(),
66
+ })
67
+ ),
68
+ pets: z
69
+ .object({
70
+ animal: z.string().nonempty(),
71
+ name: z.string().nonempty(),
72
+ })
73
+ .array()
74
+ .optional(),
75
+ })
76
+ ),
77
+ },
78
+ ];
79
+
80
+ // Not going to enforce exact error strings here
81
+ const anyString = expect.any(String);
82
+
83
+ describe("Validation", () => {
84
+ describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
85
+ describe("validate", () => {
86
+ it("should return the data when valid", () => {
87
+ const person: Person = {
88
+ firstName: "John",
89
+ lastName: "Doe",
90
+ age: 30,
91
+ address: {
92
+ streetAddress: "123 Main St",
93
+ city: "Anytown",
94
+ country: "USA",
95
+ },
96
+ pets: [{ animal: "dog", name: "Fido" }],
97
+ };
98
+ expect(validator.validate(person)).toEqual({
99
+ data: person,
100
+ error: undefined,
101
+ });
102
+ });
103
+
104
+ it("should return field errors when invalid", () => {
105
+ const obj = { age: "hi!", pets: [{ animal: "dog" }] };
106
+ expect(validator.validate(obj)).toEqual({
107
+ data: undefined,
108
+ error: {
109
+ firstName: anyString,
110
+ lastName: anyString,
111
+ age: anyString,
112
+ "address.city": anyString,
113
+ "address.country": anyString,
114
+ "address.streetAddress": anyString,
115
+ "pets[0].name": anyString,
116
+ _submittedData: obj,
117
+ },
118
+ });
119
+ });
120
+
121
+ it("should unflatten data when validating", () => {
122
+ const data = {
123
+ firstName: "John",
124
+ lastName: "Doe",
125
+ age: 30,
126
+ "address.streetAddress": "123 Main St",
127
+ "address.city": "Anytown",
128
+ "address.country": "USA",
129
+ "pets[0].animal": "dog",
130
+ "pets[0].name": "Fido",
131
+ };
132
+ expect(validator.validate(data)).toEqual({
133
+ data: {
134
+ firstName: "John",
135
+ lastName: "Doe",
136
+ age: 30,
137
+ address: {
138
+ streetAddress: "123 Main St",
139
+ city: "Anytown",
140
+ country: "USA",
141
+ },
142
+ pets: [{ animal: "dog", name: "Fido" }],
143
+ },
144
+ error: undefined,
145
+ });
146
+ });
147
+
148
+ it("should accept FormData directly and return errors", () => {
149
+ const formData = new TestFormData();
150
+ formData.set("firstName", "John");
151
+ formData.set("lastName", "Doe");
152
+ formData.set("address.streetAddress", "123 Main St");
153
+ formData.set("address.country", "USA");
154
+ formData.set("pets[0].animal", "dog");
155
+
156
+ expect(validator.validate(formData)).toEqual({
157
+ data: undefined,
158
+ error: {
159
+ "address.city": anyString,
160
+ "pets[0].name": anyString,
161
+ _submittedData: objectFromPathEntries([...formData.entries()]),
162
+ },
163
+ });
164
+ });
165
+
166
+ it("should accept FormData directly and return valid data", () => {
167
+ const formData = new TestFormData();
168
+ formData.set("firstName", "John");
169
+ formData.set("lastName", "Doe");
170
+ formData.set("address.streetAddress", "123 Main St");
171
+ formData.set("address.country", "USA");
172
+ formData.set("address.city", "Anytown");
173
+ formData.set("pets[0].animal", "dog");
174
+ formData.set("pets[0].name", "Fido");
175
+
176
+ expect(validator.validate(formData)).toEqual({
177
+ data: {
178
+ firstName: "John",
179
+ lastName: "Doe",
180
+ address: {
181
+ streetAddress: "123 Main St",
182
+ country: "USA",
183
+ city: "Anytown",
184
+ },
185
+ pets: [{ animal: "dog", name: "Fido" }],
186
+ },
187
+ error: undefined,
188
+ });
189
+ });
190
+ });
191
+
192
+ describe("validateField", () => {
193
+ it("should not return an error if field is valid", () => {
194
+ const person = {
195
+ firstName: "John",
196
+ lastName: {}, // invalid, but we should only be validating firstName
197
+ };
198
+ expect(validator.validateField(person, "firstName")).toEqual({
199
+ error: undefined,
200
+ });
201
+ });
202
+ it("should not return an error if a nested field is valid", () => {
203
+ const person = {
204
+ firstName: "John",
205
+ lastName: {}, // invalid, but we should only be validating firstName
206
+ address: {
207
+ streetAddress: "123 Main St",
208
+ city: "Anytown",
209
+ country: "USA",
210
+ },
211
+ pets: [{ animal: "dog", name: "Fido" }],
212
+ };
213
+ expect(
214
+ validator.validateField(person, "address.streetAddress")
215
+ ).toEqual({
216
+ error: undefined,
217
+ });
218
+ expect(validator.validateField(person, "address.city")).toEqual({
219
+ error: undefined,
220
+ });
221
+ expect(validator.validateField(person, "address.country")).toEqual({
222
+ error: undefined,
223
+ });
224
+ expect(validator.validateField(person, "pets[0].animal")).toEqual({
225
+ error: undefined,
226
+ });
227
+ expect(validator.validateField(person, "pets[0].name")).toEqual({
228
+ error: undefined,
229
+ });
230
+ });
231
+
232
+ it("should return an error if field is invalid", () => {
233
+ const person = {
234
+ firstName: "John",
235
+ lastName: {},
236
+ address: {
237
+ streetAddress: "123 Main St",
238
+ city: 1234,
239
+ },
240
+ };
241
+ expect(validator.validateField(person, "lastName")).toEqual({
242
+ error: anyString,
243
+ });
244
+ });
245
+
246
+ it("should return an error if a nested field is invalid", () => {
247
+ const person = {
248
+ firstName: "John",
249
+ lastName: {},
250
+ address: {
251
+ streetAddress: "123 Main St",
252
+ city: 1234,
253
+ },
254
+ pets: [{ animal: "dog" }],
255
+ };
256
+ expect(validator.validateField(person, "address.country")).toEqual({
257
+ error: anyString,
258
+ });
259
+ expect(validator.validateField(person, "pets[0].name")).toEqual({
260
+ error: anyString,
261
+ });
262
+ });
263
+ });
264
+ });
265
+ });
266
+
267
+ describe("withZod", () => {
268
+ it("returns coherent errors for complex schemas", () => {
269
+ const schema = z.union([
270
+ z.object({
271
+ type: z.literal("foo"),
272
+ foo: z.string(),
273
+ }),
274
+ z.object({
275
+ type: z.literal("bar"),
276
+ bar: z.string(),
277
+ }),
278
+ ]);
279
+ const obj = {
280
+ type: "foo",
281
+ bar: 123,
282
+ foo: 123,
283
+ };
284
+
285
+ expect(withZod(schema).validate(obj)).toEqual({
286
+ data: undefined,
287
+ error: {
288
+ type: anyString,
289
+ bar: anyString,
290
+ foo: anyString,
291
+ _submittedData: obj,
292
+ },
293
+ });
294
+ });
295
+
296
+ it("returns errors for fields that are unions", () => {
297
+ const schema = z.object({
298
+ field1: z.union([z.literal("foo"), z.literal("bar")]),
299
+ field2: z.union([z.literal("foo"), z.literal("bar")]),
300
+ });
301
+ const obj = {
302
+ field1: "a value",
303
+ // field2 missing
304
+ };
305
+
306
+ const validator = withZod(schema);
307
+ expect(validator.validate(obj)).toEqual({
308
+ data: undefined,
309
+ error: {
310
+ field1: anyString,
311
+ field2: anyString,
312
+ _submittedData: obj,
313
+ },
314
+ });
315
+ expect(validator.validateField(obj, "field1")).toEqual({
316
+ error: anyString,
317
+ });
318
+ expect(validator.validateField(obj, "field2")).toEqual({
319
+ error: anyString,
320
+ });
321
+ });
322
+ });
@@ -0,0 +1,43 @@
1
+ import type { AnyObjectSchema, InferType, ValidationError } from "yup";
2
+ import { createValidator } from "./createValidator";
3
+ import { FieldErrors, Validator } from "./types";
4
+
5
+ const validationErrorToFieldErrors = (error: ValidationError): FieldErrors => {
6
+ const fieldErrors: FieldErrors = {};
7
+ error.inner.forEach((innerError) => {
8
+ if (!innerError.path) return;
9
+ fieldErrors[innerError.path] = innerError.message;
10
+ });
11
+ return fieldErrors;
12
+ };
13
+
14
+ /**
15
+ * Create a `Validator` using a `yup` schema.
16
+ */
17
+ export const withYup = <Schema extends AnyObjectSchema>(
18
+ validationSchema: Schema
19
+ ): Validator<InferType<Schema>> => {
20
+ return createValidator({
21
+ validate: (data) => {
22
+ try {
23
+ const validated = validationSchema.validateSync(data, {
24
+ abortEarly: false,
25
+ });
26
+ return { data: validated, error: undefined };
27
+ } catch (err) {
28
+ return {
29
+ error: validationErrorToFieldErrors(err as ValidationError),
30
+ data: undefined,
31
+ };
32
+ }
33
+ },
34
+ validateField: (data, field) => {
35
+ try {
36
+ validationSchema.validateSyncAt(field, data);
37
+ return {};
38
+ } catch (err) {
39
+ return { error: (err as ValidationError).message };
40
+ }
41
+ },
42
+ });
43
+ };
@@ -0,0 +1,51 @@
1
+ import isEqual from "lodash/isEqual";
2
+ import toPath from "lodash/toPath";
3
+ import type { z } from "zod";
4
+ import { FieldErrors, Validator } from "..";
5
+ import { createValidator } from "./createValidator";
6
+
7
+ const getIssuesForError = (err: z.ZodError<any>): z.ZodIssue[] => {
8
+ return err.issues.flatMap((issue) => {
9
+ if ("unionErrors" in issue) {
10
+ return issue.unionErrors.flatMap((err) => getIssuesForError(err));
11
+ } else {
12
+ return [issue];
13
+ }
14
+ });
15
+ };
16
+
17
+ function pathToString(array: (string | number)[]): string {
18
+ return array.reduce(function (string: string, item: string | number) {
19
+ var prefix = string === "" ? "" : ".";
20
+ return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
21
+ }, "");
22
+ }
23
+
24
+ /**
25
+ * Create a validator using a `zod` schema.
26
+ */
27
+ export function withZod<T>(zodSchema: z.Schema<T>): Validator<T> {
28
+ return createValidator({
29
+ validate: (value) => {
30
+ const result = zodSchema.safeParse(value);
31
+ if (result.success) return { data: result.data, error: undefined };
32
+
33
+ const fieldErrors: FieldErrors = {};
34
+ getIssuesForError(result.error).forEach((issue) => {
35
+ const path = pathToString(issue.path);
36
+ if (!fieldErrors[path]) fieldErrors[path] = issue.message;
37
+ });
38
+ return { error: fieldErrors, data: undefined };
39
+ },
40
+ validateField: (data, field) => {
41
+ const result = zodSchema.safeParse(data);
42
+ if (result.success) return { error: undefined };
43
+ return {
44
+ error: getIssuesForError(result.error).find((issue) => {
45
+ const allPathsAsString = issue.path.map((p) => `${p}`);
46
+ return isEqual(allPathsAsString, toPath(field));
47
+ })?.message,
48
+ };
49
+ },
50
+ });
51
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "tsconfig/tsconfig.json",
3
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
4
+ "exclude": ["node_modules"]
5
+ }
package/.eslintcache DELETED
@@ -1 +0,0 @@
1
- [{"/Users/aaronpettengill/dev/remix-validated-form/src/index.ts":"1"},{"size":306,"mtime":1639405886301,"results":"2","hashOfConfig":"3"},{"filePath":"4","messages":"5","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1p84lfm","/Users/aaronpettengill/dev/remix-validated-form/src/index.ts",[]]
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- ./package-lock.json
package/.prettierignore DELETED
@@ -1,10 +0,0 @@
1
- .cache
2
- build
3
- browser
4
- test-app/build
5
- test-app/remix-validated-form
6
- sample-app/build
7
- sample-app/remix-validated-form
8
- node_modules
9
- public
10
- package-lock.json
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Aaron Pettengill
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,4 +0,0 @@
1
- import { GenericObject } from ".";
2
- export declare const objectFromPathEntries: (entries: [string, any][]) => {};
3
- /** Flatten an object so there are no nested objects or arrays */
4
- export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
@@ -1,35 +0,0 @@
1
- // `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
2
- import assign from "lodash/assign";
3
- import isArray from "lodash/isArray";
4
- import isObject from "lodash/isObject";
5
- import keys from "lodash/keys";
6
- import mapKeys from "lodash/mapKeys";
7
- import set from "lodash/set";
8
- import transform from "lodash/transform";
9
- export const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => set(acc, key, value), {});
10
- /** Flatten an object so there are no nested objects or arrays */
11
- export function flatten(obj, preserveEmpty = false) {
12
- return transform(obj, function (result, value, key) {
13
- if (isObject(value)) {
14
- let flatMap = mapKeys(flatten(value, preserveEmpty), function (_mvalue, mkey) {
15
- if (isArray(value)) {
16
- let index = mkey.indexOf(".");
17
- if (-1 !== index) {
18
- return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
19
- }
20
- return `${key}[${mkey}]`;
21
- }
22
- return `${key}.${mkey}`;
23
- });
24
- assign(result, flatMap);
25
- // Preverve Empty arrays and objects
26
- if (preserveEmpty && keys(flatMap).length === 0) {
27
- result[key] = value;
28
- }
29
- }
30
- else {
31
- result[key] = value;
32
- }
33
- return result;
34
- }, {});
35
- }
@@ -1,4 +0,0 @@
1
- import { GenericObject } from ".";
2
- export declare const objectFromPathEntries: (entries: [string, any][]) => {};
3
- /** Flatten an object so there are no nested objects or arrays */
4
- export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
package/build/flatten.js DELETED
@@ -1,43 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.flatten = exports.objectFromPathEntries = void 0;
7
- // `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
8
- const assign_1 = __importDefault(require("lodash/assign"));
9
- const isArray_1 = __importDefault(require("lodash/isArray"));
10
- const isObject_1 = __importDefault(require("lodash/isObject"));
11
- const keys_1 = __importDefault(require("lodash/keys"));
12
- const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
13
- const set_1 = __importDefault(require("lodash/set"));
14
- const transform_1 = __importDefault(require("lodash/transform"));
15
- const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
16
- exports.objectFromPathEntries = objectFromPathEntries;
17
- /** Flatten an object so there are no nested objects or arrays */
18
- function flatten(obj, preserveEmpty = false) {
19
- return (0, transform_1.default)(obj, function (result, value, key) {
20
- if ((0, isObject_1.default)(value)) {
21
- let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
22
- if ((0, isArray_1.default)(value)) {
23
- let index = mkey.indexOf(".");
24
- if (-1 !== index) {
25
- return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
26
- }
27
- return `${key}[${mkey}]`;
28
- }
29
- return `${key}.${mkey}`;
30
- });
31
- (0, assign_1.default)(result, flatMap);
32
- // Preverve Empty arrays and objects
33
- if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
34
- result[key] = value;
35
- }
36
- }
37
- else {
38
- result[key] = value;
39
- }
40
- return result;
41
- }, {});
42
- }
43
- exports.flatten = flatten;
package/sample-app/.env DELETED
@@ -1,7 +0,0 @@
1
- # Environment variables declared in this file are automatically made available to Prisma.
2
- # See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
3
-
4
- # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
5
- # See the documentation for all the connection string options: https://pris.ly/d/connection-strings
6
-
7
- DATABASE_URL="file:./dev.db"
@@ -1,53 +0,0 @@
1
- # Welcome to Remix!
2
-
3
- - [Remix Docs](https://remix.run/docs)
4
-
5
- ## Development
6
-
7
- From your terminal:
8
-
9
- ```sh
10
- npm run dev
11
- ```
12
-
13
- This starts your app in development mode, rebuilding assets on file changes.
14
-
15
- ## Deployment
16
-
17
- First, build your app for production:
18
-
19
- ```sh
20
- npm run build
21
- ```
22
-
23
- Then run the app in production mode:
24
-
25
- ```sh
26
- npm start
27
- ```
28
-
29
- Now you'll need to pick a host to deploy it to.
30
-
31
- ### DIY
32
-
33
- If you're familiar with deploying node applications, the built-in Remix app server is production-ready.
34
-
35
- Make sure to deploy the output of `remix build`
36
-
37
- - `build/`
38
- - `public/build/`
39
-
40
- ### Using a Template
41
-
42
- When you ran `npx create-remix@latest` there were a few choices for hosting. You can run that again to create a new project, then copy over your `app/` folder to the new project that's pre-configured for your target server.
43
-
44
- ```sh
45
- cd ..
46
- # create a new project, and pick a pre-configured host
47
- npx create-remix@latest
48
- cd my-new-remix-app
49
- # remove the new project's app (not the old one!)
50
- rm -rf app
51
- # copy your app over
52
- cp -R ../my-old-remix-app/app app
53
- ```
@@ -1,34 +0,0 @@
1
- import {
2
- Alert,
3
- AlertDescription,
4
- AlertIcon,
5
- AlertTitle,
6
- } from "@chakra-ui/react";
7
-
8
- type ErrorProps = {
9
- title?: React.ReactNode;
10
- description?: React.ReactNode;
11
- };
12
- export function ErrorBox({ title, description }: ErrorProps) {
13
- return (
14
- <Alert
15
- status="error"
16
- variant="subtle"
17
- flexDirection="column"
18
- alignItems="center"
19
- justifyContent="center"
20
- textAlign="center"
21
- height="200px"
22
- >
23
- <AlertIcon boxSize="40px" mr={0} />
24
- {title && (
25
- <AlertTitle mt={4} mb={1} fontSize="lg">
26
- {title}
27
- </AlertTitle>
28
- )}
29
- {description && (
30
- <AlertDescription maxWidth="sm">{description}</AlertDescription>
31
- )}
32
- </Alert>
33
- );
34
- }