@equinor/apollo-utils 0.1.0 → 0.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.
@@ -5,10 +5,10 @@ $ tsup src/index.ts --format esm,cjs --dts --external react
5
5
  CLI Target: es6
6
6
  ESM Build start
7
7
  CJS Build start
8
- CJS dist/index.js 3.75 KB
9
- CJS ⚡️ Build success in 65ms
10
- ESM dist/index.mjs 2.62 KB
11
- ESM ⚡️ Build success in 68ms
8
+ ESM dist/index.mjs 3.48 KB
9
+ ESM ⚡️ Build success in 85ms
10
+ CJS dist/index.js 4.62 KB
11
+ CJS ⚡️ Build success in 86ms
12
12
  DTS Build start
13
- DTS ⚡️ Build success in 3380ms
14
- DTS dist/index.d.ts 1.86 KB
13
+ DTS ⚡️ Build success in 4462ms
14
+ DTS dist/index.d.ts 2.00 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @equinor/apollo-utils
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - f9e695e: Add validateAsync utility to support async refinements
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -9,6 +9,8 @@ type ValidationErrorMap<T> = Map<keyof T, {
9
9
 
10
10
  declare function createValidator<S extends z.ZodTypeAny>(schema: S): {
11
11
  validate: <E extends z.TypeOf<S>>(entity: E) => ValidationErrorMap<E> | undefined;
12
+ validateAsync: <E_1 extends z.TypeOf<S>>(entity: z.infer<typeof schema>) => Promise<ValidationErrorMap<E_1> | undefined>;
13
+ getSchema(): S;
12
14
  };
13
15
 
14
16
  type FormState<T> = {
package/dist/index.js CHANGED
@@ -30,6 +30,26 @@ var __copyProps = (to, from, except, desc) => {
30
30
  return to;
31
31
  };
32
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+ var __async = (__this, __arguments, generator) => {
34
+ return new Promise((resolve, reject) => {
35
+ var fulfilled = (value) => {
36
+ try {
37
+ step(generator.next(value));
38
+ } catch (e) {
39
+ reject(e);
40
+ }
41
+ };
42
+ var rejected = (value) => {
43
+ try {
44
+ step(generator.throw(value));
45
+ } catch (e) {
46
+ reject(e);
47
+ }
48
+ };
49
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
50
+ step((generator = generator.apply(__this, __arguments)).next());
51
+ });
52
+ };
33
53
 
34
54
  // src/index.ts
35
55
  var src_exports = {};
@@ -97,15 +117,27 @@ function createValidator(schema) {
97
117
  const validation = schema.safeParse(entity);
98
118
  if (validation.success)
99
119
  return void 0;
100
- return new Map(
101
- validation.error.errors.map((error) => [
102
- error.path[0],
103
- { message: error.message, code: error.code }
104
- ])
105
- );
120
+ return prepareErrors(validation);
121
+ },
122
+ validateAsync: (entity) => __async(this, null, function* () {
123
+ const validation = yield schema.safeParseAsync(entity);
124
+ if (validation.success)
125
+ return void 0;
126
+ return prepareErrors(validation);
127
+ }),
128
+ getSchema() {
129
+ return schema;
106
130
  }
107
131
  };
108
132
  }
133
+ function prepareErrors(errorValidation) {
134
+ return new Map(
135
+ errorValidation.error.errors.map((error) => [
136
+ error.path[0],
137
+ { message: error.message, code: error.code }
138
+ ])
139
+ );
140
+ }
109
141
  // Annotate the CommonJS export names for ESM import in node:
110
142
  0 && (module.exports = {
111
143
  createFormFamily,
package/dist/index.mjs CHANGED
@@ -14,6 +14,26 @@ var __spreadValues = (a, b) => {
14
14
  }
15
15
  return a;
16
16
  };
17
+ var __async = (__this, __arguments, generator) => {
18
+ return new Promise((resolve, reject) => {
19
+ var fulfilled = (value) => {
20
+ try {
21
+ step(generator.next(value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ };
26
+ var rejected = (value) => {
27
+ try {
28
+ step(generator.throw(value));
29
+ } catch (e) {
30
+ reject(e);
31
+ }
32
+ };
33
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
+ step((generator = generator.apply(__this, __arguments)).next());
35
+ });
36
+ };
17
37
 
18
38
  // src/jotai-form/formUtils.ts
19
39
  import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
@@ -71,15 +91,27 @@ function createValidator(schema) {
71
91
  const validation = schema.safeParse(entity);
72
92
  if (validation.success)
73
93
  return void 0;
74
- return new Map(
75
- validation.error.errors.map((error) => [
76
- error.path[0],
77
- { message: error.message, code: error.code }
78
- ])
79
- );
94
+ return prepareErrors(validation);
95
+ },
96
+ validateAsync: (entity) => __async(this, null, function* () {
97
+ const validation = yield schema.safeParseAsync(entity);
98
+ if (validation.success)
99
+ return void 0;
100
+ return prepareErrors(validation);
101
+ }),
102
+ getSchema() {
103
+ return schema;
80
104
  }
81
105
  };
82
106
  }
107
+ function prepareErrors(errorValidation) {
108
+ return new Map(
109
+ errorValidation.error.errors.map((error) => [
110
+ error.path[0],
111
+ { message: error.message, code: error.code }
112
+ ])
113
+ );
114
+ }
83
115
  export {
84
116
  createFormFamily,
85
117
  createValidator,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@equinor/apollo-utils",
3
3
  "main": "src/index.ts",
4
4
  "types": "src/index.ts",
5
- "version": "0.1.0",
5
+ "version": "0.1.1",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "tsup src/index.ts --format esm,cjs --dts --external react",
@@ -6,12 +6,24 @@ export function createValidator<S extends z.ZodTypeAny>(schema: S) {
6
6
  validate: <E extends z.infer<typeof schema>>(entity: E) => {
7
7
  const validation = schema.safeParse(entity)
8
8
  if (validation.success) return undefined
9
- return new Map(
10
- validation.error.errors.map((error) => [
11
- error.path[0] as keyof E,
12
- { message: error.message, code: error.code },
13
- ])
14
- ) as ValidationErrorMap<E>
9
+ return prepareErrors<E>(validation)
10
+ },
11
+ validateAsync: async <E extends z.infer<typeof schema>>(entity: z.infer<typeof schema>) => {
12
+ const validation = await schema.safeParseAsync(entity)
13
+ if (validation.success) return undefined
14
+ return prepareErrors<E>(validation)
15
+ },
16
+ getSchema() {
17
+ return schema
15
18
  },
16
19
  }
17
20
  }
21
+
22
+ function prepareErrors<E extends Record<string, unknown>>(errorValidation: z.SafeParseError<E>) {
23
+ return new Map(
24
+ errorValidation.error.errors.map((error) => [
25
+ error.path[0] as keyof E,
26
+ { message: error.message, code: error.code },
27
+ ])
28
+ ) as ValidationErrorMap<E>
29
+ }