@standard-community/standard-form 0.0.1 → 0.1.0

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,139 +1,113 @@
1
- import { a as UnsupportedSchemaError } from '../utils-BxGdbpqk.js';
1
+ import { a as UnsupportedSchemaError, b as hasFormDataKeyOrNested, h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
2
2
 
3
3
  async function zod() {
4
- return (schema, formData) => {
5
- if (!isSchema(schema)) {
4
+ return (schema, formData, options) => {
5
+ if (!isZodSchema(schema)) {
6
6
  throw new UnsupportedSchemaError("zod", "Zod 4 and higher is required");
7
7
  }
8
- const objectSchema = getObjectSchema(schema, formData);
9
- return objectSchema ? convertObject(formData, objectSchema) : formData;
8
+ const baseSchema = unwrapBaseObjectSchema(schema, formData);
9
+ return hasZodType(baseSchema, "object") ? formDataToObject(formData, baseSchema, "", options) : formData;
10
10
  };
11
11
  }
12
- function isRecord(value) {
13
- return typeof value === "object" && value !== null;
12
+ function isZodSchema(schema) {
13
+ return typeof schema === "object" && schema !== null && "_zod" in schema;
14
14
  }
15
- function get(value, key) {
16
- return isRecord(value) ? value[key] : void 0;
17
- }
18
- function definition(schema) {
19
- const internal = get(schema, "_zod");
20
- const def = get(internal, "def");
21
- return isRecord(def) ? def : void 0;
22
- }
23
- function isSchema(value) {
24
- return typeof get(definition(value), "type") === "string";
25
- }
26
- function schemaType(schema) {
27
- const type = get(definition(schema), "type");
28
- return typeof type === "string" ? type : void 0;
29
- }
30
- function unwrap(schema) {
31
- while (true) {
32
- const kind = schemaType(schema);
33
- const def = definition(schema);
34
- if (!def) return schema;
35
- if (kind === "optional" || kind === "nullable" || kind === "default") {
36
- schema = def.innerType;
37
- } else if (kind === "pipe") {
38
- schema = def.in;
39
- } else {
40
- return schema;
41
- }
42
- }
15
+ function hasZodType(schema, type) {
16
+ return isZodSchema(schema) && schema._zod.def.type === type;
43
17
  }
44
18
  function isDiscriminatedUnion(schema) {
45
- return schemaType(schema) === "union" && typeof get(definition(schema), "discriminator") === "string";
46
- }
47
- function objectShape(schema) {
48
- const shape = get(definition(schema), "shape");
49
- return isRecord(shape) ? shape : void 0;
19
+ return schema._zod.def.type === "union" && "discriminator" in schema._zod.def;
50
20
  }
51
- function getObjectSchema(schema, formData) {
52
- const base = unwrap(schema);
53
- if (schemaType(base) === "object") return base;
54
- if (!isDiscriminatedUnion(base)) return;
55
- const def = definition(base);
56
- if (!def) return;
57
- const discriminator = def.discriminator;
58
- if (typeof discriminator !== "string") return;
59
- const value = formData.get(discriminator);
60
- if (typeof value !== "string" || !Array.isArray(def.options)) return;
61
- return def.options.find((option) => {
62
- const literal = objectShape(option)?.[discriminator];
63
- return schemaType(literal) === "literal" && containsLiteral(literal, value);
64
- });
65
- }
66
- function convertObject(formData, schema, prefix = "") {
67
- const result = {};
68
- const keys = [...formData.keys()];
69
- const def = definition(schema);
70
- const shape = objectShape(schema);
71
- if (!def || !shape) return result;
72
- if (def.catchall) {
73
- for (const [key, value] of formData.entries()) {
74
- if (key.startsWith(prefix)) result[key.slice(prefix.length)] = value;
21
+ function formDataToObject(formData, schema, prefix = "", options = {}) {
22
+ const formKeys = [...formData.keys()];
23
+ const obj = schema._zod.def.catchall ? Object.fromEntries(
24
+ [...formData.entries()].filter(([k]) => k.startsWith(prefix)).map(([k, v]) => [k.slice(prefix.length), v])
25
+ ) : {};
26
+ for (const [key, baseValidator] of Object.entries(schema._zod.def.shape)) {
27
+ const prefixedKey = prefix + key;
28
+ let validator = baseValidator;
29
+ while (hasZodType(validator, "optional") || hasZodType(validator, "nullable") || hasZodType(validator, "default")) {
30
+ if (hasZodType(validator, "default") && !hasFormDataKeyOrNested(formKeys, prefixedKey)) {
31
+ obj[key] = validator._zod.def.defaultValue instanceof Function ? validator._zod.def.defaultValue() : validator._zod.def.defaultValue;
32
+ }
33
+ validator = validator._zod.def.innerType;
75
34
  }
76
- }
77
- for (const [key, original] of Object.entries(shape)) {
78
- const name = prefix + key;
79
- let child = original;
80
- let defaultValue;
81
- while (isSchema(child)) {
82
- const kind = schemaType(child);
83
- const childDef = definition(child);
84
- if (!childDef) break;
85
- if (kind === "default") {
86
- const value = childDef.defaultValue;
87
- defaultValue = typeof value === "function" ? value() : value;
88
- child = childDef.innerType;
89
- } else if (kind === "optional" || kind === "nullable" || kind === "pipe") {
90
- child = childDef.innerType ?? childDef.in;
91
- } else {
92
- break;
35
+ while (hasZodType(validator, "pipe")) {
36
+ validator = validator._zod.def.in;
37
+ }
38
+ if (isDiscriminatedUnion(validator)) {
39
+ const typeKey = validator._zod.def.discriminator;
40
+ const typeValue = formData.get(prefixedKey + "." + typeKey);
41
+ if (typeof typeValue === "string") {
42
+ const match = validator._zod.def.options.find(
43
+ (option) => option.def.shape[typeKey].values.has(typeValue)
44
+ );
45
+ if (match) {
46
+ validator = match;
47
+ }
93
48
  }
94
49
  }
95
- if (defaultValue !== void 0 && !hasKeyOrChild(keys, name)) result[key] = defaultValue;
96
- if (schemaType(child) === "object") {
97
- const nestedPrefix = `${name}.`;
98
- if (keys.some((formKey) => formKey.startsWith(nestedPrefix))) {
99
- result[key] = convertObject(formData, child, nestedPrefix);
100
- } else if (!(key in result)) {
101
- result[key] = schemaType(original) === "nullable" ? null : void 0;
50
+ if (hasZodType(validator, "object")) {
51
+ const nestedPrefix = prefixedKey + ".";
52
+ const hasNestedKeys = hasFormDataKeyWithPrefix(formKeys, nestedPrefix);
53
+ if (hasNestedKeys) {
54
+ obj[key] = formDataToObject(formData, validator, nestedPrefix, options);
55
+ } else if (!(key in obj)) {
56
+ obj[key] = hasZodType(baseValidator, "nullable") ? null : void 0;
102
57
  }
103
- } else if (isDiscriminatedUnion(child)) {
104
- const union = definition(child);
105
- const discriminator = union?.discriminator;
106
- const value = typeof discriminator === "string" ? formData.get(`${name}.${discriminator}`) : null;
107
- const options = union?.options;
108
- const variant = Array.isArray(options) && typeof discriminator === "string" ? options.find((option) => {
109
- const literal = objectShape(option)?.[discriminator];
110
- return schemaType(literal) === "literal" && containsLiteral(literal, value);
111
- }) : void 0;
112
- result[key] = variant ? convertObject(formData, variant, `${name}.`) : null;
113
- } else if (schemaType(child) === "boolean") {
114
- const value = formData.get(name);
115
- result[key] = value === "true" ? true : value === "false" ? false : formData.has(name);
116
- } else if (schemaType(child) === "array") {
117
- const element = get(definition(child), "element");
118
- result[key] = formData.getAll(name).map((value) => convertValue(value, element));
119
- } else if (!(defaultValue !== void 0 && !formData.has(name))) {
120
- const value = formData.get(name);
121
- result[key] = value === null ? schemaType(original) === "optional" ? void 0 : null : convertValue(value, child);
58
+ } else if (!formData.has(prefixedKey) && key in obj) {
59
+ continue;
60
+ } else if (hasZodType(validator, "boolean")) {
61
+ const val = formData.get(prefixedKey);
62
+ obj[key] = isTrueFormValue(val) ? true : val === "false" ? false : formData.has(prefixedKey);
63
+ } else if (hasZodType(validator, "array")) {
64
+ obj[key] = handleFormDataGetAll(prefixedKey, formData, validator, options);
65
+ } else {
66
+ obj[key] = handleFormDataGet(prefixedKey, formData, validator, baseValidator, options);
122
67
  }
123
68
  }
124
- return result;
69
+ return obj;
125
70
  }
126
- function hasKeyOrChild(keys, name) {
127
- return keys.some((key) => key === name || key.startsWith(`${name}.`));
71
+ function handleFormDataGetAll(key, formData, validator, options) {
72
+ const entries = Array.from(formData.getAll(key));
73
+ const elementValidator = validator._zod.def.element;
74
+ if (hasZodType(elementValidator, "number")) {
75
+ return entries.map((value) => coerceFormNumber(value));
76
+ } else if (hasZodType(elementValidator, "boolean")) {
77
+ return entries.map(
78
+ (v) => isTrueFormValue(v) ? true : v === "false" ? false : Boolean(v)
79
+ );
80
+ }
81
+ if (options.convertEmptyStringsToNull && hasZodType(elementValidator, "string")) {
82
+ return entries.map((value) => value === "" ? null : value);
83
+ }
84
+ return entries;
128
85
  }
129
- function containsLiteral(schema, value) {
130
- const values = get(definition(schema), "values");
131
- return Array.isArray(values) ? values.includes(value) : values instanceof Set && values.has(value);
86
+ function handleFormDataGet(key, formData, validator, baseValidator, options) {
87
+ const value = formData.get(key);
88
+ if (!value) {
89
+ return hasZodType(baseValidator, "optional") ? void 0 : null;
90
+ }
91
+ if (options.convertEmptyStringsToNull && value === "" && hasZodType(validator, "string")) {
92
+ return null;
93
+ }
94
+ return hasZodType(validator, "number") ? coerceFormNumber(value) : value;
132
95
  }
133
- function convertValue(value, schema) {
134
- if (schemaType(schema) === "number") return value === "" ? value : Number(value);
135
- if (schemaType(schema) === "boolean") return value === "true";
136
- return value;
96
+ function unwrapBaseObjectSchema(schema, unparsedInput) {
97
+ if (hasZodType(schema, "pipe")) {
98
+ return unwrapBaseObjectSchema(schema._zod.def.in, unparsedInput);
99
+ }
100
+ if (isDiscriminatedUnion(schema)) {
101
+ const typeKey = schema._zod.def.discriminator;
102
+ const typeValue = unparsedInput.get(typeKey);
103
+ if (typeof typeValue !== "string") return schema;
104
+ const objSchema = schema._zod.def.options.find(
105
+ (option) => option.def.shape[typeKey].values.has(typeValue)
106
+ );
107
+ if (!objSchema) return schema;
108
+ return objSchema;
109
+ }
110
+ return schema;
137
111
  }
138
112
 
139
113
  export { zod as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@standard-community/standard-form",
3
3
  "description": "Parse FormData with any Standard Schema validator.",
4
- "version": "0.0.1",
4
+ "version": "0.1.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.cjs",
@@ -47,21 +47,37 @@
47
47
  "types": "./dist/vendors/zod.d.cts",
48
48
  "default": "./dist/vendors/zod.cjs"
49
49
  }
50
+ },
51
+ "./valibot": {
52
+ "import": {
53
+ "types": "./dist/vendors/valibot.d.ts",
54
+ "default": "./dist/vendors/valibot.js"
55
+ },
56
+ "require": {
57
+ "types": "./dist/vendors/valibot.d.cts",
58
+ "default": "./dist/vendors/valibot.cjs"
59
+ }
60
+ },
61
+ "./arktype": {
62
+ "import": {
63
+ "types": "./dist/vendors/arktype.d.ts",
64
+ "default": "./dist/vendors/arktype.js"
65
+ },
66
+ "require": {
67
+ "types": "./dist/vendors/arktype.d.cts",
68
+ "default": "./dist/vendors/arktype.cjs"
69
+ }
50
70
  }
51
71
  },
52
72
  "peerDependencies": {
53
- "@standard-schema/spec": "^1.1.0",
54
- "zod": "^4.3.6"
55
- },
56
- "peerDependenciesMeta": {
57
- "zod": {
58
- "optional": true
59
- }
73
+ "@standard-schema/spec": "^1.1.0"
60
74
  },
61
75
  "devDependencies": {
62
76
  "@standard-schema/spec": "^1.1.0",
77
+ "arktype": "^2.2.10",
63
78
  "pkgroll": "^2.29.1",
64
79
  "typescript": "6.0.3",
80
+ "valibot": "^1.5.0",
65
81
  "vitest": "^5.1.1",
66
82
  "zod": "^4.6.7"
67
83
  },
@@ -1,18 +0,0 @@
1
- const validationMapper = /* @__PURE__ */ new Map();
2
- class UnsupportedVendorError extends Error {
3
- constructor(vendor) {
4
- super(`standard-form: Unsupported schema vendor "${vendor}".`);
5
- }
6
- }
7
- class MissingDependencyError extends Error {
8
- constructor(packageName) {
9
- super(`standard-form: Missing dependencies "${packageName}".`);
10
- }
11
- }
12
- class UnsupportedSchemaError extends Error {
13
- constructor(vendor, requirement) {
14
- super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
15
- }
16
- }
17
-
18
- export { MissingDependencyError as M, UnsupportedVendorError as U, UnsupportedSchemaError as a, validationMapper as v };
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- const validationMapper = /* @__PURE__ */ new Map();
4
- class UnsupportedVendorError extends Error {
5
- constructor(vendor) {
6
- super(`standard-form: Unsupported schema vendor "${vendor}".`);
7
- }
8
- }
9
- class MissingDependencyError extends Error {
10
- constructor(packageName) {
11
- super(`standard-form: Missing dependencies "${packageName}".`);
12
- }
13
- }
14
- class UnsupportedSchemaError extends Error {
15
- constructor(vendor, requirement) {
16
- super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
17
- }
18
- }
19
-
20
- exports.MissingDependencyError = MissingDependencyError;
21
- exports.UnsupportedSchemaError = UnsupportedSchemaError;
22
- exports.UnsupportedVendorError = UnsupportedVendorError;
23
- exports.validationMapper = validationMapper;