@buildnbuzz/buzzform 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.
package/dist/index.js CHANGED
@@ -22,7 +22,6 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  FormConfigContext: () => FormConfigContext,
24
24
  FormProvider: () => FormProvider,
25
- applyCustomValidation: () => applyCustomValidation,
26
25
  applyNumericPrecision: () => applyNumericPrecision,
27
26
  clampNumber: () => clampNumber,
28
27
  coerceToDate: () => coerceToDate,
@@ -125,59 +124,26 @@ function extractValidationConfig(validate) {
125
124
  }
126
125
  return { fn: void 0, isLive: false };
127
126
  }
128
- function applyCustomValidation(schema, field, fieldPath = "") {
129
- if (!("validate" in field)) {
130
- return schema;
131
- }
132
- const fieldWithValidate = field;
133
- if (!fieldWithValidate.validate) {
134
- return schema;
135
- }
136
- const config = extractValidationConfig(fieldWithValidate.validate);
137
- if (!config.fn) {
138
- return schema;
139
- }
140
- return schema.superRefine(async (val, ctx) => {
141
- const result = await config.fn(val, {
142
- data: {},
143
- siblingData: {},
144
- path: fieldPath.split(".")
145
- });
146
- if (result !== true) {
147
- ctx.addIssue({
148
- code: import_zod.z.ZodIssueCode.custom,
149
- message: typeof result === "string" ? result : "Validation failed"
150
- });
151
- }
152
- });
153
- }
154
127
  function makeOptional(schema, fieldType) {
155
128
  switch (fieldType) {
156
- // String types: allow empty string
157
129
  case "text":
158
130
  case "textarea":
159
131
  case "email":
160
132
  case "password":
161
133
  return schema.optional().or(import_zod.z.literal(""));
162
- // Nullable types
163
134
  case "number":
164
135
  case "date":
165
136
  case "select":
166
137
  case "radio":
167
138
  return schema.optional().nullable();
168
- // Boolean types: always have a value
169
139
  case "checkbox":
170
140
  case "switch":
171
141
  return schema;
172
- // Booleans are never "optional" in the traditional sense
173
- // Array types
174
142
  case "tags":
175
143
  case "array":
176
144
  return schema.optional().default([]);
177
- // Upload
178
145
  case "upload":
179
146
  return schema.optional().nullable();
180
- // Default
181
147
  default:
182
148
  return schema.optional();
183
149
  }
@@ -234,25 +200,25 @@ function isFileTypeAccepted(file, accept) {
234
200
 
235
201
  // src/schema/builders/text.ts
236
202
  function createTextFieldSchema(field) {
237
- let schema = import_zod2.z.string();
203
+ let schema = import_zod2.z.string({ error: "This field is required" });
238
204
  if ("pattern" in field && field.pattern) {
239
205
  const pattern = typeof field.pattern === "string" ? new RegExp(field.pattern) : field.pattern;
240
206
  schema = schema.regex(pattern, {
241
- message: getPatternErrorMessage(field.pattern)
207
+ error: getPatternErrorMessage(field.pattern)
242
208
  });
243
209
  }
244
210
  if (field.minLength) {
245
211
  schema = schema.min(field.minLength, {
246
- message: `Must be at least ${field.minLength} characters`
212
+ error: `Must be at least ${field.minLength} characters`
247
213
  });
248
214
  }
249
215
  if (field.maxLength) {
250
216
  schema = schema.max(field.maxLength, {
251
- message: `Must be no more than ${field.maxLength} characters`
217
+ error: `Must be no more than ${field.maxLength} characters`
252
218
  });
253
219
  }
254
220
  if (field.required) {
255
- schema = schema.min(1, { message: "This field is required" });
221
+ schema = schema.min(1, { error: "This field is required" });
256
222
  }
257
223
  let finalSchema = schema;
258
224
  if ("trim" in field && field.trim) {
@@ -260,67 +226,63 @@ function createTextFieldSchema(field) {
260
226
  return typeof val === "string" ? val.trim() : val;
261
227
  }, schema);
262
228
  }
263
- finalSchema = applyCustomValidation(finalSchema, field, field.name);
264
229
  if (!field.required) {
265
230
  return makeOptional(finalSchema, field.type);
266
231
  }
267
232
  return finalSchema;
268
233
  }
269
234
  function createEmailFieldSchema(field) {
270
- let schema = import_zod2.z.string().email({ message: "Invalid email address" });
235
+ let schema = import_zod2.z.email({ error: "Invalid email address" });
271
236
  if (field.minLength) {
272
237
  schema = schema.min(field.minLength, {
273
- message: `Must be at least ${field.minLength} characters`
238
+ error: `Must be at least ${field.minLength} characters`
274
239
  });
275
240
  }
276
241
  if (field.maxLength) {
277
242
  schema = schema.max(field.maxLength, {
278
- message: `Must be no more than ${field.maxLength} characters`
243
+ error: `Must be no more than ${field.maxLength} characters`
279
244
  });
280
245
  }
281
246
  if (field.required) {
282
- schema = schema.min(1, { message: "Email is required" });
247
+ schema = schema.min(1, { error: "Email is required" });
283
248
  }
284
- const finalSchema = applyCustomValidation(schema, field, field.name);
285
249
  if (!field.required) {
286
- return makeOptional(finalSchema, "email");
250
+ return makeOptional(schema, "email");
287
251
  }
288
- return finalSchema;
252
+ return schema;
289
253
  }
290
254
  function createPasswordFieldSchema(field) {
291
- let schema = import_zod2.z.string();
255
+ let schema = import_zod2.z.string({ error: "Password is required" });
292
256
  if (field.minLength) {
293
257
  schema = schema.min(field.minLength, {
294
- message: `Password must be at least ${field.minLength} characters`
258
+ error: `Password must be at least ${field.minLength} characters`
295
259
  });
296
260
  }
297
261
  if (field.maxLength) {
298
262
  schema = schema.max(field.maxLength, {
299
- message: `Password must be no more than ${field.maxLength} characters`
263
+ error: `Password must be no more than ${field.maxLength} characters`
300
264
  });
301
265
  }
302
266
  if (field.required) {
303
- schema = schema.min(1, { message: "Password is required" });
267
+ schema = schema.min(1, { error: "Password is required" });
304
268
  }
305
- const finalSchema = applyCustomValidation(schema, field, field.name);
306
269
  if (!field.required) {
307
- return makeOptional(finalSchema, "password");
270
+ return makeOptional(schema, "password");
308
271
  }
309
- return finalSchema;
272
+ return schema;
310
273
  }
311
274
 
312
275
  // src/schema/builders/number.ts
313
276
  var import_zod3 = require("zod");
314
277
  function createNumberFieldSchema(field) {
315
- let numSchema = import_zod3.z.number({ invalid_type_error: "Must be a number" });
278
+ let numSchema = import_zod3.z.number({ error: "Must be a number" });
316
279
  if (field.min !== void 0) {
317
280
  numSchema = numSchema.min(field.min, `Must be at least ${field.min}`);
318
281
  }
319
282
  if (field.max !== void 0) {
320
283
  numSchema = numSchema.max(field.max, `Must be at most ${field.max}`);
321
284
  }
322
- let schema = import_zod3.z.preprocess(coerceToNumber, numSchema);
323
- schema = applyCustomValidation(schema, field, field.name);
285
+ const schema = import_zod3.z.preprocess(coerceToNumber, numSchema);
324
286
  if (field.required) {
325
287
  return schema;
326
288
  }
@@ -339,21 +301,20 @@ function createDateFieldSchema(field) {
339
301
  const isDatetime = field.type === "datetime";
340
302
  const minDate = toDate(field.minDate);
341
303
  const maxDate = toDate(field.maxDate);
342
- let dateSchema = import_zod4.z.date({ invalid_type_error: "Please enter a valid date" });
304
+ let dateSchema = import_zod4.z.date({ error: "Please enter a valid date" });
343
305
  if (minDate) {
344
306
  const formattedDate = isDatetime ? minDate.toLocaleString() : minDate.toDateString();
345
307
  dateSchema = dateSchema.min(minDate, {
346
- message: `Date must be on or after ${formattedDate}`
308
+ error: `Date must be on or after ${formattedDate}`
347
309
  });
348
310
  }
349
311
  if (maxDate) {
350
312
  const formattedDate = isDatetime ? maxDate.toLocaleString() : maxDate.toDateString();
351
313
  dateSchema = dateSchema.max(maxDate, {
352
- message: `Date must be on or before ${formattedDate}`
314
+ error: `Date must be on or before ${formattedDate}`
353
315
  });
354
316
  }
355
- let schema = import_zod4.z.preprocess(coerceToDate, dateSchema);
356
- schema = applyCustomValidation(schema, field, field.name);
317
+ const schema = import_zod4.z.preprocess(coerceToDate, dateSchema);
357
318
  if (field.required) {
358
319
  return schema.refine(
359
320
  (val) => val instanceof Date && !isNaN(val.getTime()),
@@ -365,18 +326,21 @@ function createDateFieldSchema(field) {
365
326
 
366
327
  // src/schema/builders/select.ts
367
328
  var import_zod5 = require("zod");
368
- var selectValueSchema = import_zod5.z.union([import_zod5.z.string(), import_zod5.z.number(), import_zod5.z.boolean()]);
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" });
369
334
  function createSelectFieldSchema(field) {
370
335
  if (field.hasMany) {
371
- let arraySchema = import_zod5.z.array(selectValueSchema);
336
+ let arraySchema = import_zod5.z.array(selectValueSchema, { error: "Invalid selection" });
372
337
  if (field.required) {
373
338
  arraySchema = arraySchema.min(1, "Select at least one option");
374
339
  }
375
- const schema2 = applyCustomValidation(arraySchema, field, field.name);
376
340
  if (!field.required) {
377
- return schema2.optional().default([]);
341
+ return arraySchema.optional().default([]);
378
342
  }
379
- return schema2;
343
+ return arraySchema;
380
344
  }
381
345
  let schema = selectValueSchema;
382
346
  if (field.required) {
@@ -385,7 +349,6 @@ function createSelectFieldSchema(field) {
385
349
  "Please select an option"
386
350
  );
387
351
  }
388
- schema = applyCustomValidation(schema, field, field.name);
389
352
  if (!field.required) {
390
353
  return makeOptional(schema, "select");
391
354
  }
@@ -399,7 +362,6 @@ function createRadioFieldSchema(field) {
399
362
  "Please select an option"
400
363
  );
401
364
  }
402
- schema = applyCustomValidation(schema, field, field.name);
403
365
  if (!field.required) {
404
366
  return makeOptional(schema, "radio");
405
367
  }
@@ -409,146 +371,113 @@ function createRadioFieldSchema(field) {
409
371
  // src/schema/builders/boolean.ts
410
372
  var import_zod6 = require("zod");
411
373
  function createCheckboxFieldSchema(field) {
412
- let schema = import_zod6.z.boolean();
374
+ let schema = import_zod6.z.boolean({ error: "Invalid value" });
413
375
  if (field.required) {
414
- schema = import_zod6.z.boolean().refine((val) => val === true, {
415
- message: "This field is required"
376
+ schema = import_zod6.z.boolean({ error: "This field is required" }).refine((val) => val === true, {
377
+ error: "This field is required"
416
378
  });
417
379
  }
418
- return applyCustomValidation(schema, field, field.name);
380
+ return schema;
419
381
  }
420
382
  function createSwitchFieldSchema(field) {
421
- let schema = import_zod6.z.boolean();
383
+ let schema = import_zod6.z.boolean({ error: "Invalid value" });
422
384
  if (field.required) {
423
- schema = import_zod6.z.boolean().refine((val) => val === true, {
424
- message: "This field is required"
385
+ schema = import_zod6.z.boolean({ error: "This field is required" }).refine((val) => val === true, {
386
+ error: "This field is required"
425
387
  });
426
388
  }
427
- return applyCustomValidation(schema, field, field.name);
389
+ return schema;
428
390
  }
429
391
 
430
392
  // src/schema/builders/upload.ts
431
393
  var import_zod7 = require("zod");
432
- function validateSingleFile(value, field) {
433
- if (value === null || value === void 0 || value === "") {
434
- return { valid: true };
435
- }
436
- if (typeof value === "string") {
437
- return { valid: true };
394
+ function matchesMimePattern(fileType, pattern) {
395
+ const normalizedPattern = pattern.toLowerCase().trim();
396
+ const normalizedType = fileType.toLowerCase();
397
+ if (normalizedPattern.endsWith("/*")) {
398
+ const category = normalizedPattern.replace("/*", "");
399
+ return normalizedType.startsWith(category + "/");
438
400
  }
439
- if (!isFileLike(value)) {
440
- return { valid: false, message: "Invalid file" };
401
+ if (normalizedPattern.startsWith(".")) {
402
+ return true;
441
403
  }
442
- if (field.maxSize && value.size > field.maxSize) {
404
+ return normalizedType === normalizedPattern;
405
+ }
406
+ function isFileTypeAccepted2(file, acceptPatterns) {
407
+ if (!file.type) return true;
408
+ return acceptPatterns.some((pattern) => matchesMimePattern(file.type, pattern));
409
+ }
410
+ function createUploadFieldSchema(field) {
411
+ let fileSchema = import_zod7.z.file({ error: "Please select a file" });
412
+ if (field.maxSize) {
443
413
  const sizeMB = (field.maxSize / 1024 / 1024).toFixed(1);
444
- return {
445
- valid: false,
446
- message: `File must be smaller than ${sizeMB}MB`
447
- };
414
+ fileSchema = fileSchema.max(field.maxSize, {
415
+ error: `File must be smaller than ${sizeMB}MB`
416
+ });
448
417
  }
449
418
  const accept = field.ui?.accept;
450
419
  if (accept && accept !== "*") {
451
- if (!isFileTypeAccepted(value, accept)) {
452
- return {
453
- valid: false,
454
- message: `File type not allowed. Accepted: ${accept}`
455
- };
420
+ const acceptPatterns = accept.split(",").map((t) => t.trim());
421
+ const hasWildcard = acceptPatterns.some((p) => p.includes("*") || p.startsWith("."));
422
+ if (hasWildcard) {
423
+ fileSchema = fileSchema.refine(
424
+ (file) => isFileTypeAccepted2(file, acceptPatterns),
425
+ `File type not allowed. Accepted: ${accept}`
426
+ );
427
+ } else {
428
+ fileSchema = fileSchema.mime(acceptPatterns, {
429
+ error: `File type not allowed. Accepted: ${accept}`
430
+ });
456
431
  }
457
432
  }
458
- return { valid: true };
459
- }
460
- function createUploadFieldSchema(field) {
433
+ const fileOrUrl = import_zod7.z.union([
434
+ fileSchema,
435
+ import_zod7.z.string({ error: "Invalid file" })
436
+ ], { error: "Please select a file" });
461
437
  if (field.hasMany) {
462
- const schema2 = import_zod7.z.array(import_zod7.z.any()).superRefine((files, ctx) => {
463
- for (let i = 0; i < files.length; i++) {
464
- const file = files[i];
465
- const result = validateSingleFile(file, field);
466
- if (!result.valid) {
467
- ctx.addIssue({
468
- code: import_zod7.z.ZodIssueCode.custom,
469
- message: result.message || "Invalid file",
470
- path: [i]
471
- });
472
- }
473
- }
474
- if (field.required) {
475
- const validFiles = files.filter((f) => f !== null && f !== void 0);
476
- if (validFiles.length === 0) {
477
- ctx.addIssue({
478
- code: import_zod7.z.ZodIssueCode.custom,
479
- message: "At least one file is required"
480
- });
481
- }
482
- }
483
- if (field.minFiles !== void 0 && field.minFiles > 0) {
484
- const validFiles = files.filter((f) => f !== null && f !== void 0);
485
- if (validFiles.length < field.minFiles) {
486
- ctx.addIssue({
487
- code: import_zod7.z.ZodIssueCode.custom,
488
- message: `At least ${field.minFiles} file(s) required`
489
- });
490
- }
491
- }
492
- if (field.maxFiles !== void 0) {
493
- const validFiles = files.filter((f) => f !== null && f !== void 0);
494
- if (validFiles.length > field.maxFiles) {
495
- ctx.addIssue({
496
- code: import_zod7.z.ZodIssueCode.custom,
497
- message: `Maximum ${field.maxFiles} file(s) allowed`
498
- });
499
- }
500
- }
501
- });
502
- if (!field.required) {
503
- return schema2.optional().default([]);
504
- }
505
- return schema2;
506
- }
507
- const schema = import_zod7.z.any().superRefine((value, ctx) => {
508
- if (field.required && (value === null || value === void 0 || value === "")) {
509
- ctx.addIssue({
510
- code: import_zod7.z.ZodIssueCode.custom,
511
- message: "File is required"
438
+ let arraySchema = import_zod7.z.array(fileOrUrl, { error: "Invalid files" });
439
+ if (field.minFiles !== void 0 && field.minFiles > 0) {
440
+ arraySchema = arraySchema.min(field.minFiles, {
441
+ error: `At least ${field.minFiles} file(s) required`
512
442
  });
513
- return;
514
443
  }
515
- if (value === null || value === void 0 || value === "") {
516
- return;
444
+ if (field.maxFiles !== void 0) {
445
+ arraySchema = arraySchema.max(field.maxFiles, {
446
+ error: `Maximum ${field.maxFiles} file(s) allowed`
447
+ });
517
448
  }
518
- const result = validateSingleFile(value, field);
519
- if (!result.valid) {
520
- ctx.addIssue({
521
- code: import_zod7.z.ZodIssueCode.custom,
522
- message: result.message || "Invalid file"
449
+ if (field.required) {
450
+ arraySchema = arraySchema.min(1, {
451
+ error: "At least one file is required"
523
452
  });
453
+ return arraySchema;
524
454
  }
525
- });
526
- if (!field.required) {
527
- return schema.optional().nullable();
455
+ return arraySchema.optional().default([]);
528
456
  }
529
- return schema;
457
+ if (field.required) {
458
+ return fileOrUrl;
459
+ }
460
+ return fileOrUrl.optional().nullable();
530
461
  }
531
462
 
532
463
  // src/schema/builders/tags.ts
533
464
  var import_zod8 = require("zod");
534
465
  function createTagsFieldSchema(field) {
535
- const tagSchema = import_zod8.z.string();
536
- let schema = import_zod8.z.array(tagSchema);
466
+ const tagSchema = import_zod8.z.string({ error: "Invalid tag" });
467
+ let schema = import_zod8.z.array(tagSchema, { error: "Invalid tags" });
537
468
  if (field.minTags !== void 0) {
538
469
  schema = schema.min(field.minTags, `At least ${field.minTags} tag(s) required`);
539
470
  }
540
471
  if (field.maxTags !== void 0) {
541
472
  schema = schema.max(field.maxTags, `Maximum ${field.maxTags} tag(s) allowed`);
542
473
  }
543
- let finalSchema = applyCustomValidation(schema, field, field.name);
544
474
  if (field.required) {
545
- finalSchema = finalSchema.refine(
475
+ return schema.refine(
546
476
  (arr) => Array.isArray(arr) && arr.length > 0,
547
477
  "At least one tag is required"
548
478
  );
549
- return finalSchema;
550
479
  }
551
- return finalSchema.optional().default([]);
480
+ return schema.optional().default([]);
552
481
  }
553
482
 
554
483
  // src/schema/builders/composite.ts
@@ -587,7 +516,6 @@ function fieldToZod(field) {
587
516
  return field.schema;
588
517
  }
589
518
  switch (field.type) {
590
- // Text-based fields
591
519
  case "text":
592
520
  return createTextFieldSchema(field);
593
521
  case "email":
@@ -596,35 +524,27 @@ function fieldToZod(field) {
596
524
  return createPasswordFieldSchema(field);
597
525
  case "textarea":
598
526
  return createTextFieldSchema(field);
599
- // Number
600
527
  case "number":
601
528
  return createNumberFieldSchema(field);
602
- // Date
603
529
  case "date":
604
530
  case "datetime":
605
531
  return createDateFieldSchema(field);
606
- // Selection
607
532
  case "select":
608
533
  return createSelectFieldSchema(field);
609
534
  case "radio":
610
535
  return createRadioFieldSchema(field);
611
- // Boolean
612
536
  case "checkbox":
613
537
  return createCheckboxFieldSchema(field);
614
538
  case "switch":
615
539
  return createSwitchFieldSchema(field);
616
- // Upload
617
540
  case "upload":
618
541
  return createUploadFieldSchema(field);
619
- // Tags
620
542
  case "tags":
621
543
  return createTagsFieldSchema(field);
622
- // Composite (recursive)
623
544
  case "array":
624
545
  return createArrayFieldSchema(field, fieldsToZodSchema);
625
546
  case "group":
626
547
  return createGroupFieldSchema(field, fieldsToZodSchema);
627
- // Layout fields don't produce schemas directly
628
548
  case "row":
629
549
  case "collapsible":
630
550
  case "tabs":
@@ -992,7 +912,6 @@ function useForm(options) {
992
912
  0 && (module.exports = {
993
913
  FormConfigContext,
994
914
  FormProvider,
995
- applyCustomValidation,
996
915
  applyNumericPrecision,
997
916
  clampNumber,
998
917
  coerceToDate,