@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.
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ var utils = require('../utils-BoRc22Ql.cjs');
4
+
5
+ async function valibot() {
6
+ return (schema, formData, options) => {
7
+ if (!isValibotSchema(schema)) return formData;
8
+ return isObjectSchema(schema) ? convertObject(formData, schema, "", options) : formData;
9
+ };
10
+ }
11
+ function isValibotSchema(schema) {
12
+ return typeof schema === "object" && schema !== null && "~standard" in schema;
13
+ }
14
+ function isObjectSchema(schema) {
15
+ return schema.type === "object" || schema.type === "object_with_rest" || schema.type === "loose_object" || schema.type === "strict_object";
16
+ }
17
+ function isSchema(schema) {
18
+ return typeof schema === "object" && schema !== null && "kind" in schema && "type" in schema;
19
+ }
20
+ function isArraySchema(schema) {
21
+ return schema.type === "array";
22
+ }
23
+ function isVariantSchema(schema) {
24
+ return schema.type === "variant";
25
+ }
26
+ function isLiteralSchema(schema) {
27
+ return schema.type === "literal";
28
+ }
29
+ function convertObject(formData, schema, prefix = "", options) {
30
+ const result = {};
31
+ for (const [key, child] of Object.entries(schema.entries)) {
32
+ const name = prefix + key;
33
+ if (!isSchema(child)) continue;
34
+ const base = unwrap(child);
35
+ if (isObjectSchema(base)) {
36
+ const nested = convertObject(formData, base, `${name}.`, options);
37
+ const hasNestedKeys = utils.hasFormDataKeyWithPrefix(formData.keys(), `${name}.`);
38
+ result[key] = hasNestedKeys ? nested : getDefault(child) ?? nested;
39
+ } else if (base.type === "boolean") {
40
+ const value = formData.get(name);
41
+ result[key] = utils.isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
42
+ } else if (base.type === "number") {
43
+ const value = formData.get(name);
44
+ result[key] = utils.coerceFormNumber(value);
45
+ } else if (base.type === "array") {
46
+ if (!isArraySchema(base)) continue;
47
+ result[key] = formData.getAll(name).map((value) => convertValue(value, base.item, options));
48
+ } else if (base.type === "variant") {
49
+ if (!isVariantSchema(base)) continue;
50
+ const variant = base;
51
+ const discriminator = formData.get(`${name}.${variant.key}`);
52
+ const option = variant.options.find((candidate) => {
53
+ if (!isObjectSchema(candidate)) return false;
54
+ const discriminatorSchema = candidate.entries[variant.key];
55
+ return isSchema(discriminatorSchema) && isLiteralSchema(discriminatorSchema) && discriminatorSchema.literal === discriminator;
56
+ });
57
+ result[key] = option && isObjectSchema(option) ? convertObject(formData, option, `${name}.`, options) : null;
58
+ } else {
59
+ result[key] = convertValue(formData.get(name), base, options);
60
+ }
61
+ }
62
+ if (schema.type === "object_with_rest") {
63
+ formData.forEach((value, key) => {
64
+ if (key.startsWith(prefix) && !(key.slice(prefix.length) in result)) {
65
+ result[key.slice(prefix.length)] = value;
66
+ }
67
+ });
68
+ }
69
+ return result;
70
+ }
71
+ function unwrap(schema) {
72
+ if ("wrapped" in schema && isSchema(schema.wrapped)) return unwrap(schema.wrapped);
73
+ if ("pipe" in schema) {
74
+ const pipe = schema.pipe;
75
+ if (Array.isArray(pipe) && isSchema(pipe[0])) return unwrap(pipe[0]);
76
+ }
77
+ return schema;
78
+ }
79
+ function getDefault(schema) {
80
+ return "default" in schema ? schema.default : void 0;
81
+ }
82
+ function convertValue(value, schema, options) {
83
+ const base = unwrap(schema);
84
+ if (options.convertEmptyStringsToNull && value === "" && base.type === "string") return null;
85
+ if (value === null) return getDefault(schema) ?? null;
86
+ if (base.type === "number") return utils.coerceFormNumber(value);
87
+ if (base.type === "boolean") {
88
+ return utils.isTrueFormValue(value) ? true : value === "false" ? false : Boolean(value);
89
+ }
90
+ return value;
91
+ }
92
+
93
+ module.exports = valibot;
@@ -0,0 +1,5 @@
1
+ import { ToFormInputFn } from '../index.cjs';
2
+
3
+ declare function export_default(): Promise<ToFormInputFn>;
4
+
5
+ export { export_default as default };
@@ -0,0 +1,5 @@
1
+ import { ToFormInputFn } from '../index.js';
2
+
3
+ declare function export_default(): Promise<ToFormInputFn>;
4
+
5
+ export { export_default as default };
@@ -0,0 +1,91 @@
1
+ import { h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
2
+
3
+ async function valibot() {
4
+ return (schema, formData, options) => {
5
+ if (!isValibotSchema(schema)) return formData;
6
+ return isObjectSchema(schema) ? convertObject(formData, schema, "", options) : formData;
7
+ };
8
+ }
9
+ function isValibotSchema(schema) {
10
+ return typeof schema === "object" && schema !== null && "~standard" in schema;
11
+ }
12
+ function isObjectSchema(schema) {
13
+ return schema.type === "object" || schema.type === "object_with_rest" || schema.type === "loose_object" || schema.type === "strict_object";
14
+ }
15
+ function isSchema(schema) {
16
+ return typeof schema === "object" && schema !== null && "kind" in schema && "type" in schema;
17
+ }
18
+ function isArraySchema(schema) {
19
+ return schema.type === "array";
20
+ }
21
+ function isVariantSchema(schema) {
22
+ return schema.type === "variant";
23
+ }
24
+ function isLiteralSchema(schema) {
25
+ return schema.type === "literal";
26
+ }
27
+ function convertObject(formData, schema, prefix = "", options) {
28
+ const result = {};
29
+ for (const [key, child] of Object.entries(schema.entries)) {
30
+ const name = prefix + key;
31
+ if (!isSchema(child)) continue;
32
+ const base = unwrap(child);
33
+ if (isObjectSchema(base)) {
34
+ const nested = convertObject(formData, base, `${name}.`, options);
35
+ const hasNestedKeys = hasFormDataKeyWithPrefix(formData.keys(), `${name}.`);
36
+ result[key] = hasNestedKeys ? nested : getDefault(child) ?? nested;
37
+ } else if (base.type === "boolean") {
38
+ const value = formData.get(name);
39
+ result[key] = isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
40
+ } else if (base.type === "number") {
41
+ const value = formData.get(name);
42
+ result[key] = coerceFormNumber(value);
43
+ } else if (base.type === "array") {
44
+ if (!isArraySchema(base)) continue;
45
+ result[key] = formData.getAll(name).map((value) => convertValue(value, base.item, options));
46
+ } else if (base.type === "variant") {
47
+ if (!isVariantSchema(base)) continue;
48
+ const variant = base;
49
+ const discriminator = formData.get(`${name}.${variant.key}`);
50
+ const option = variant.options.find((candidate) => {
51
+ if (!isObjectSchema(candidate)) return false;
52
+ const discriminatorSchema = candidate.entries[variant.key];
53
+ return isSchema(discriminatorSchema) && isLiteralSchema(discriminatorSchema) && discriminatorSchema.literal === discriminator;
54
+ });
55
+ result[key] = option && isObjectSchema(option) ? convertObject(formData, option, `${name}.`, options) : null;
56
+ } else {
57
+ result[key] = convertValue(formData.get(name), base, options);
58
+ }
59
+ }
60
+ if (schema.type === "object_with_rest") {
61
+ formData.forEach((value, key) => {
62
+ if (key.startsWith(prefix) && !(key.slice(prefix.length) in result)) {
63
+ result[key.slice(prefix.length)] = value;
64
+ }
65
+ });
66
+ }
67
+ return result;
68
+ }
69
+ function unwrap(schema) {
70
+ if ("wrapped" in schema && isSchema(schema.wrapped)) return unwrap(schema.wrapped);
71
+ if ("pipe" in schema) {
72
+ const pipe = schema.pipe;
73
+ if (Array.isArray(pipe) && isSchema(pipe[0])) return unwrap(pipe[0]);
74
+ }
75
+ return schema;
76
+ }
77
+ function getDefault(schema) {
78
+ return "default" in schema ? schema.default : void 0;
79
+ }
80
+ function convertValue(value, schema, options) {
81
+ const base = unwrap(schema);
82
+ if (options.convertEmptyStringsToNull && value === "" && base.type === "string") return null;
83
+ if (value === null) return getDefault(schema) ?? null;
84
+ if (base.type === "number") return coerceFormNumber(value);
85
+ if (base.type === "boolean") {
86
+ return isTrueFormValue(value) ? true : value === "false" ? false : Boolean(value);
87
+ }
88
+ return value;
89
+ }
90
+
91
+ export { valibot as default };
@@ -1,141 +1,115 @@
1
1
  'use strict';
2
2
 
3
- var utils = require('../utils-f6FAxdZk.cjs');
3
+ var utils = require('../utils-BoRc22Ql.cjs');
4
4
 
5
5
  async function zod() {
6
- return (schema, formData) => {
7
- if (!isSchema(schema)) {
6
+ return (schema, formData, options) => {
7
+ if (!isZodSchema(schema)) {
8
8
  throw new utils.UnsupportedSchemaError("zod", "Zod 4 and higher is required");
9
9
  }
10
- const objectSchema = getObjectSchema(schema, formData);
11
- return objectSchema ? convertObject(formData, objectSchema) : formData;
10
+ const baseSchema = unwrapBaseObjectSchema(schema, formData);
11
+ return hasZodType(baseSchema, "object") ? formDataToObject(formData, baseSchema, "", options) : formData;
12
12
  };
13
13
  }
14
- function isRecord(value) {
15
- return typeof value === "object" && value !== null;
14
+ function isZodSchema(schema) {
15
+ return typeof schema === "object" && schema !== null && "_zod" in schema;
16
16
  }
17
- function get(value, key) {
18
- return isRecord(value) ? value[key] : void 0;
19
- }
20
- function definition(schema) {
21
- const internal = get(schema, "_zod");
22
- const def = get(internal, "def");
23
- return isRecord(def) ? def : void 0;
24
- }
25
- function isSchema(value) {
26
- return typeof get(definition(value), "type") === "string";
27
- }
28
- function schemaType(schema) {
29
- const type = get(definition(schema), "type");
30
- return typeof type === "string" ? type : void 0;
31
- }
32
- function unwrap(schema) {
33
- while (true) {
34
- const kind = schemaType(schema);
35
- const def = definition(schema);
36
- if (!def) return schema;
37
- if (kind === "optional" || kind === "nullable" || kind === "default") {
38
- schema = def.innerType;
39
- } else if (kind === "pipe") {
40
- schema = def.in;
41
- } else {
42
- return schema;
43
- }
44
- }
17
+ function hasZodType(schema, type) {
18
+ return isZodSchema(schema) && schema._zod.def.type === type;
45
19
  }
46
20
  function isDiscriminatedUnion(schema) {
47
- return schemaType(schema) === "union" && typeof get(definition(schema), "discriminator") === "string";
48
- }
49
- function objectShape(schema) {
50
- const shape = get(definition(schema), "shape");
51
- return isRecord(shape) ? shape : void 0;
21
+ return schema._zod.def.type === "union" && "discriminator" in schema._zod.def;
52
22
  }
53
- function getObjectSchema(schema, formData) {
54
- const base = unwrap(schema);
55
- if (schemaType(base) === "object") return base;
56
- if (!isDiscriminatedUnion(base)) return;
57
- const def = definition(base);
58
- if (!def) return;
59
- const discriminator = def.discriminator;
60
- if (typeof discriminator !== "string") return;
61
- const value = formData.get(discriminator);
62
- if (typeof value !== "string" || !Array.isArray(def.options)) return;
63
- return def.options.find((option) => {
64
- const literal = objectShape(option)?.[discriminator];
65
- return schemaType(literal) === "literal" && containsLiteral(literal, value);
66
- });
67
- }
68
- function convertObject(formData, schema, prefix = "") {
69
- const result = {};
70
- const keys = [...formData.keys()];
71
- const def = definition(schema);
72
- const shape = objectShape(schema);
73
- if (!def || !shape) return result;
74
- if (def.catchall) {
75
- for (const [key, value] of formData.entries()) {
76
- if (key.startsWith(prefix)) result[key.slice(prefix.length)] = value;
23
+ function formDataToObject(formData, schema, prefix = "", options = {}) {
24
+ const formKeys = [...formData.keys()];
25
+ const obj = schema._zod.def.catchall ? Object.fromEntries(
26
+ [...formData.entries()].filter(([k]) => k.startsWith(prefix)).map(([k, v]) => [k.slice(prefix.length), v])
27
+ ) : {};
28
+ for (const [key, baseValidator] of Object.entries(schema._zod.def.shape)) {
29
+ const prefixedKey = prefix + key;
30
+ let validator = baseValidator;
31
+ while (hasZodType(validator, "optional") || hasZodType(validator, "nullable") || hasZodType(validator, "default")) {
32
+ if (hasZodType(validator, "default") && !utils.hasFormDataKeyOrNested(formKeys, prefixedKey)) {
33
+ obj[key] = validator._zod.def.defaultValue instanceof Function ? validator._zod.def.defaultValue() : validator._zod.def.defaultValue;
34
+ }
35
+ validator = validator._zod.def.innerType;
77
36
  }
78
- }
79
- for (const [key, original] of Object.entries(shape)) {
80
- const name = prefix + key;
81
- let child = original;
82
- let defaultValue;
83
- while (isSchema(child)) {
84
- const kind = schemaType(child);
85
- const childDef = definition(child);
86
- if (!childDef) break;
87
- if (kind === "default") {
88
- const value = childDef.defaultValue;
89
- defaultValue = typeof value === "function" ? value() : value;
90
- child = childDef.innerType;
91
- } else if (kind === "optional" || kind === "nullable" || kind === "pipe") {
92
- child = childDef.innerType ?? childDef.in;
93
- } else {
94
- break;
37
+ while (hasZodType(validator, "pipe")) {
38
+ validator = validator._zod.def.in;
39
+ }
40
+ if (isDiscriminatedUnion(validator)) {
41
+ const typeKey = validator._zod.def.discriminator;
42
+ const typeValue = formData.get(prefixedKey + "." + typeKey);
43
+ if (typeof typeValue === "string") {
44
+ const match = validator._zod.def.options.find(
45
+ (option) => option.def.shape[typeKey].values.has(typeValue)
46
+ );
47
+ if (match) {
48
+ validator = match;
49
+ }
95
50
  }
96
51
  }
97
- if (defaultValue !== void 0 && !hasKeyOrChild(keys, name)) result[key] = defaultValue;
98
- if (schemaType(child) === "object") {
99
- const nestedPrefix = `${name}.`;
100
- if (keys.some((formKey) => formKey.startsWith(nestedPrefix))) {
101
- result[key] = convertObject(formData, child, nestedPrefix);
102
- } else if (!(key in result)) {
103
- result[key] = schemaType(original) === "nullable" ? null : void 0;
52
+ if (hasZodType(validator, "object")) {
53
+ const nestedPrefix = prefixedKey + ".";
54
+ const hasNestedKeys = utils.hasFormDataKeyWithPrefix(formKeys, nestedPrefix);
55
+ if (hasNestedKeys) {
56
+ obj[key] = formDataToObject(formData, validator, nestedPrefix, options);
57
+ } else if (!(key in obj)) {
58
+ obj[key] = hasZodType(baseValidator, "nullable") ? null : void 0;
104
59
  }
105
- } else if (isDiscriminatedUnion(child)) {
106
- const union = definition(child);
107
- const discriminator = union?.discriminator;
108
- const value = typeof discriminator === "string" ? formData.get(`${name}.${discriminator}`) : null;
109
- const options = union?.options;
110
- const variant = Array.isArray(options) && typeof discriminator === "string" ? options.find((option) => {
111
- const literal = objectShape(option)?.[discriminator];
112
- return schemaType(literal) === "literal" && containsLiteral(literal, value);
113
- }) : void 0;
114
- result[key] = variant ? convertObject(formData, variant, `${name}.`) : null;
115
- } else if (schemaType(child) === "boolean") {
116
- const value = formData.get(name);
117
- result[key] = value === "true" ? true : value === "false" ? false : formData.has(name);
118
- } else if (schemaType(child) === "array") {
119
- const element = get(definition(child), "element");
120
- result[key] = formData.getAll(name).map((value) => convertValue(value, element));
121
- } else if (!(defaultValue !== void 0 && !formData.has(name))) {
122
- const value = formData.get(name);
123
- result[key] = value === null ? schemaType(original) === "optional" ? void 0 : null : convertValue(value, child);
60
+ } else if (!formData.has(prefixedKey) && key in obj) {
61
+ continue;
62
+ } else if (hasZodType(validator, "boolean")) {
63
+ const val = formData.get(prefixedKey);
64
+ obj[key] = utils.isTrueFormValue(val) ? true : val === "false" ? false : formData.has(prefixedKey);
65
+ } else if (hasZodType(validator, "array")) {
66
+ obj[key] = handleFormDataGetAll(prefixedKey, formData, validator, options);
67
+ } else {
68
+ obj[key] = handleFormDataGet(prefixedKey, formData, validator, baseValidator, options);
124
69
  }
125
70
  }
126
- return result;
71
+ return obj;
127
72
  }
128
- function hasKeyOrChild(keys, name) {
129
- return keys.some((key) => key === name || key.startsWith(`${name}.`));
73
+ function handleFormDataGetAll(key, formData, validator, options) {
74
+ const entries = Array.from(formData.getAll(key));
75
+ const elementValidator = validator._zod.def.element;
76
+ if (hasZodType(elementValidator, "number")) {
77
+ return entries.map((value) => utils.coerceFormNumber(value));
78
+ } else if (hasZodType(elementValidator, "boolean")) {
79
+ return entries.map(
80
+ (v) => utils.isTrueFormValue(v) ? true : v === "false" ? false : Boolean(v)
81
+ );
82
+ }
83
+ if (options.convertEmptyStringsToNull && hasZodType(elementValidator, "string")) {
84
+ return entries.map((value) => value === "" ? null : value);
85
+ }
86
+ return entries;
130
87
  }
131
- function containsLiteral(schema, value) {
132
- const values = get(definition(schema), "values");
133
- return Array.isArray(values) ? values.includes(value) : values instanceof Set && values.has(value);
88
+ function handleFormDataGet(key, formData, validator, baseValidator, options) {
89
+ const value = formData.get(key);
90
+ if (!value) {
91
+ return hasZodType(baseValidator, "optional") ? void 0 : null;
92
+ }
93
+ if (options.convertEmptyStringsToNull && value === "" && hasZodType(validator, "string")) {
94
+ return null;
95
+ }
96
+ return hasZodType(validator, "number") ? utils.coerceFormNumber(value) : value;
134
97
  }
135
- function convertValue(value, schema) {
136
- if (schemaType(schema) === "number") return value === "" ? value : Number(value);
137
- if (schemaType(schema) === "boolean") return value === "true";
138
- return value;
98
+ function unwrapBaseObjectSchema(schema, unparsedInput) {
99
+ if (hasZodType(schema, "pipe")) {
100
+ return unwrapBaseObjectSchema(schema._zod.def.in, unparsedInput);
101
+ }
102
+ if (isDiscriminatedUnion(schema)) {
103
+ const typeKey = schema._zod.def.discriminator;
104
+ const typeValue = unparsedInput.get(typeKey);
105
+ if (typeof typeValue !== "string") return schema;
106
+ const objSchema = schema._zod.def.options.find(
107
+ (option) => option.def.shape[typeKey].values.has(typeValue)
108
+ );
109
+ if (!objSchema) return schema;
110
+ return objSchema;
111
+ }
112
+ return schema;
139
113
  }
140
114
 
141
115
  module.exports = zod;