@buildnbuzz/buzzform 0.1.2 → 0.1.4

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.
package/dist/index.js CHANGED
@@ -139,6 +139,8 @@ function makeOptional(schema, fieldType) {
139
139
  case "checkbox":
140
140
  case "switch":
141
141
  return schema;
142
+ case "checkbox-group":
143
+ return schema.optional().default([]);
142
144
  case "tags":
143
145
  case "array":
144
146
  return schema.optional().default([]);
@@ -326,17 +328,51 @@ function createDateFieldSchema(field) {
326
328
 
327
329
  // src/schema/builders/select.ts
328
330
  var import_zod5 = require("zod");
329
- var selectValueSchema = import_zod5.z.union([
330
- import_zod5.z.string({ error: "Please select an option" }),
331
- import_zod5.z.number({ error: "Please select an option" }),
332
- import_zod5.z.boolean({ error: "Please select an option" })
333
- ], { error: "Please select an option" });
331
+ var selectValueSchema = import_zod5.z.union(
332
+ [
333
+ import_zod5.z.string({ error: "Please select an option" }),
334
+ import_zod5.z.number({ error: "Please select an option" }),
335
+ import_zod5.z.boolean({ error: "Please select an option" })
336
+ ],
337
+ { error: "Please select an option" }
338
+ );
339
+ function applyMultiSelectConstraints(schema, config) {
340
+ const { minSelected, maxSelected, required } = config;
341
+ let next = schema;
342
+ if (minSelected !== void 0 && minSelected > 0) {
343
+ const minMsg = `Select at least ${minSelected} option${minSelected !== 1 ? "s" : ""}`;
344
+ if (required) {
345
+ next = next.min(minSelected, minMsg);
346
+ } else {
347
+ next = next.refine(
348
+ (val) => val.length === 0 || val.length >= minSelected,
349
+ {
350
+ message: minMsg
351
+ }
352
+ );
353
+ }
354
+ }
355
+ if (required && (minSelected === void 0 || minSelected === 0)) {
356
+ next = next.min(1, "Select at least one option");
357
+ }
358
+ if (maxSelected !== void 0) {
359
+ next = next.max(
360
+ maxSelected,
361
+ `Select at most ${maxSelected} option${maxSelected !== 1 ? "s" : ""}`
362
+ );
363
+ }
364
+ return next;
365
+ }
334
366
  function createSelectFieldSchema(field) {
335
367
  if (field.hasMany) {
336
- let arraySchema = import_zod5.z.array(selectValueSchema, { error: "Invalid selection" });
337
- if (field.required) {
338
- arraySchema = arraySchema.min(1, "Select at least one option");
339
- }
368
+ let arraySchema = import_zod5.z.array(selectValueSchema, {
369
+ error: "Invalid selection"
370
+ });
371
+ arraySchema = applyMultiSelectConstraints(arraySchema, {
372
+ minSelected: field.minSelected,
373
+ maxSelected: field.maxSelected,
374
+ required: field.required
375
+ });
340
376
  if (!field.required) {
341
377
  return arraySchema.optional().default([]);
342
378
  }
@@ -367,6 +403,18 @@ function createRadioFieldSchema(field) {
367
403
  }
368
404
  return schema;
369
405
  }
406
+ function createCheckboxGroupFieldSchema(field) {
407
+ let schema = import_zod5.z.array(selectValueSchema, { error: "Invalid selection" });
408
+ schema = applyMultiSelectConstraints(schema, {
409
+ minSelected: field.minSelected,
410
+ maxSelected: field.maxSelected,
411
+ required: field.required
412
+ });
413
+ if (!field.required) {
414
+ return makeOptional(schema, "checkbox-group");
415
+ }
416
+ return schema;
417
+ }
370
418
 
371
419
  // src/schema/builders/boolean.ts
372
420
  var import_zod6 = require("zod");
@@ -531,6 +579,8 @@ function fieldToZod(field) {
531
579
  return createDateFieldSchema(field);
532
580
  case "select":
533
581
  return createSelectFieldSchema(field);
582
+ case "checkbox-group":
583
+ return createCheckboxGroupFieldSchema(field);
534
584
  case "radio":
535
585
  return createRadioFieldSchema(field);
536
586
  case "checkbox":
@@ -878,34 +928,37 @@ function applySettings(form, settings) {
878
928
  }
879
929
  function useForm(options) {
880
930
  const globalConfig = (0, import_react2.useContext)(FormConfigContext);
881
- if (!options.schema) {
882
- throw new Error(
883
- "useForm: schema is required. Use createSchema([...]) to create a schema from fields, or pass a Zod schema directly."
884
- );
885
- }
886
931
  const adapter = options.adapter ?? globalConfig?.adapter;
887
932
  const resolverFn = globalConfig?.resolver;
888
933
  const mode = options.mode ?? globalConfig?.mode ?? "onChange";
889
934
  const reValidateMode = options.reValidateMode ?? globalConfig?.reValidateMode ?? "onChange";
890
- if (!adapter) {
891
- throw new Error(
892
- "useForm: No adapter configured. Either wrap your app in <FormProvider adapter={...}> or pass adapter in options."
893
- );
894
- }
895
- const resolver = resolverFn ? resolverFn(options.schema) : void 0;
935
+ const resolver = options.schema && resolverFn ? resolverFn(options.schema) : void 0;
896
936
  const schemaWithFields = options.schema;
897
937
  const fieldDefaults = (0, import_react2.useMemo)(
898
- () => extractDefaultsFromFields(schemaWithFields.fields),
899
- [schemaWithFields.fields]
938
+ () => extractDefaultsFromFields(schemaWithFields?.fields),
939
+ [schemaWithFields?.fields]
900
940
  );
901
941
  const defaultValues = options.defaultValues ?? fieldDefaults;
902
- const form = adapter({
942
+ const effectiveAdapter = adapter ?? ((_opts) => {
943
+ throw new Error("useForm: No adapter configured.");
944
+ });
945
+ const form = effectiveAdapter({
903
946
  defaultValues,
904
947
  resolver,
905
948
  mode,
906
949
  reValidateMode,
907
950
  onSubmit: options.onSubmit
908
951
  });
952
+ if (!options.schema) {
953
+ throw new Error(
954
+ "useForm: schema is required. Use createSchema([...]) to create a schema from fields, or pass a Zod schema directly."
955
+ );
956
+ }
957
+ if (!adapter) {
958
+ throw new Error(
959
+ "useForm: No adapter configured. Either wrap your app in <FormProvider adapter={...}> or pass adapter in options."
960
+ );
961
+ }
909
962
  return applySettings(form, options.settings);
910
963
  }
911
964
  // Annotate the CommonJS export names for ESM import in node: