@makehq/forman-schema 1.2.5 → 1.3.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.cjs CHANGED
@@ -21,11 +21,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  toFormanSchema: () => toFormanSchema,
24
- toJSONSchema: () => toJSONSchema
24
+ toJSONSchema: () => toJSONSchema,
25
+ validateForman: () => validateForman,
26
+ validateFormanWithDomains: () => validateFormanWithDomains
25
27
  });
26
28
  module.exports = __toCommonJS(index_exports);
27
29
 
28
30
  // src/utils.ts
31
+ var FORMAN_VISUAL_TYPES = ["banner", "markdown", "html", "separator"];
29
32
  function noEmpty(text) {
30
33
  return text?.trim() || void 0;
31
34
  }
@@ -35,6 +38,35 @@ function isObject(value) {
35
38
  function isOptionGroup(value) {
36
39
  return "options" in value && Array.isArray(value.options);
37
40
  }
41
+ function containsIMLExpression(value) {
42
+ if (typeof value !== "string") return false;
43
+ return value.indexOf("{{") > -1 && value.indexOf("}}") > -1;
44
+ }
45
+ var API_ENDPOINTS = {
46
+ account: "api://connections/{{kind}}",
47
+ aiagent: "api://ai-agents/v1/agents",
48
+ datastore: "api://data-stores",
49
+ hook: "api://hooks/{{kind}}",
50
+ keychain: "api://keys/{{kind}}",
51
+ udt: "api://data-structures"
52
+ };
53
+ function normalizeFormanFieldType(field) {
54
+ const [type, kind] = field.type.split(":");
55
+ if (!type) return field;
56
+ if (!(type in API_ENDPOINTS)) return field;
57
+ let store = isObject(field.options) ? field.options.store : field.options;
58
+ if (typeof store === "string" || Array.isArray(store)) return field;
59
+ store = API_ENDPOINTS[type];
60
+ store = store.replace("/{{kind}}", kind ? `/${kind}` : "");
61
+ return {
62
+ ...field,
63
+ type,
64
+ options: {
65
+ ...field.options,
66
+ store
67
+ }
68
+ };
69
+ }
38
70
 
39
71
  // src/forman.ts
40
72
  var SchemaConversionError = class extends Error {
@@ -49,17 +81,13 @@ var SchemaConversionError = class extends Error {
49
81
  this.name = "SchemaConversionError";
50
82
  }
51
83
  };
52
- var API_ENDPOINTS = {
53
- CONNECTIONS: "api://connections",
54
- HOOKS: "api://hooks",
55
- KEYS: "api://keys"
56
- };
57
84
  var FORMAN_TYPE_MAP = {
58
85
  account: "number",
59
86
  hook: "number",
60
87
  keychain: "number",
61
88
  datastore: "number",
62
89
  aiagent: "string",
90
+ udt: "number",
63
91
  array: "array",
64
92
  collection: "object",
65
93
  text: "string",
@@ -98,40 +126,6 @@ function validateFormanField(field) {
98
126
  throw new SchemaConversionError(`Unknown field type: ${field.type}`, field);
99
127
  }
100
128
  }
101
- function normalizeFieldType(field) {
102
- const typeHandlers = {
103
- "account:": (type) => ({
104
- ...field,
105
- type: "account",
106
- options: {
107
- ...field.options,
108
- store: `${API_ENDPOINTS.CONNECTIONS}/${type.substring(8)}`
109
- }
110
- }),
111
- "hook:": (type) => ({
112
- ...field,
113
- type: "hook",
114
- options: {
115
- ...field.options,
116
- store: `${API_ENDPOINTS.HOOKS}/${type.substring(5)}`
117
- }
118
- }),
119
- "keychain:": (type) => ({
120
- ...field,
121
- type: "keychain",
122
- options: {
123
- ...field.options,
124
- store: `${API_ENDPOINTS.KEYS}/${type.substring(9)}`
125
- }
126
- })
127
- };
128
- for (const [prefix, handler] of Object.entries(typeHandlers)) {
129
- if (field.type.startsWith(prefix)) {
130
- return handler(field.type);
131
- }
132
- }
133
- return field;
134
- }
135
129
  function appendQueryString(path, domain, tail) {
136
130
  if (path.startsWith("api://")) return path;
137
131
  const queryString = tail.map((part) => `${encodeURIComponent(part)}={{${part}}}`).join("&");
@@ -152,7 +146,7 @@ function createDefaultContext() {
152
146
  }
153
147
  function toJSONSchemaInternal(field, context = createDefaultContext()) {
154
148
  validateFormanField(field);
155
- const normalizedField = normalizeFieldType(field);
149
+ const normalizedField = normalizeFormanFieldType(field);
156
150
  const result = {
157
151
  type: FORMAN_TYPE_MAP[normalizedField.type],
158
152
  title: noEmpty(normalizedField.label),
@@ -182,6 +176,9 @@ function handleCollectionType(field, result, context) {
182
176
  required: []
183
177
  });
184
178
  function addField(subField, tail) {
179
+ if (FORMAN_VISUAL_TYPES.includes(subField.type)) {
180
+ return;
181
+ }
185
182
  if (!subField.name) return;
186
183
  if (subField.required) {
187
184
  result.required.push(subField.name);
@@ -330,7 +327,7 @@ function handlePrimitiveType(field, result) {
330
327
  }
331
328
  if (field.validate) {
332
329
  if (field.validate.pattern) {
333
- result.pattern = field.validate.pattern;
330
+ result.pattern = typeof field.validate.pattern === "object" ? field.validate.pattern.regexp : field.validate.pattern;
334
331
  }
335
332
  if (field.validate.min !== void 0) {
336
333
  result.minimum = field.validate.min;
@@ -345,6 +342,472 @@ function handlePrimitiveType(field, result) {
345
342
  return result;
346
343
  }
347
344
 
345
+ // src/validator.ts
346
+ var FORMAN_TYPE_MAP2 = {
347
+ account: "number",
348
+ hook: "number",
349
+ keychain: "number",
350
+ datastore: "number",
351
+ aiagent: "string",
352
+ udt: "number",
353
+ array: "array",
354
+ collection: "object",
355
+ text: "string",
356
+ number: "number",
357
+ boolean: "boolean",
358
+ date: "string",
359
+ json: "string",
360
+ buffer: "string",
361
+ cert: "string",
362
+ color: "string",
363
+ email: "string",
364
+ filename: "string",
365
+ file: "string",
366
+ folder: "string",
367
+ hidden: void 0,
368
+ integer: "number",
369
+ uinteger: "number",
370
+ password: "string",
371
+ path: "string",
372
+ pkey: "string",
373
+ port: "number",
374
+ select: void 0,
375
+ time: "string",
376
+ timestamp: "string",
377
+ timezone: "string",
378
+ upload: "array",
379
+ url: "string",
380
+ uuid: "string",
381
+ any: void 0
382
+ };
383
+ async function validateFormanWithDomainsInternal(domains, options) {
384
+ const errors = [];
385
+ const roots = Object.keys(domains).reduce(
386
+ (acc, domain) => {
387
+ acc[domain] = {
388
+ seenFields: /* @__PURE__ */ new Set(),
389
+ validateFields: (fields, context) => {
390
+ return validateFormanValue(
391
+ domains[domain].values,
392
+ {
393
+ name: domain,
394
+ type: "collection",
395
+ spec: fields
396
+ },
397
+ {
398
+ ...context,
399
+ path: [],
400
+ domain
401
+ }
402
+ );
403
+ }
404
+ };
405
+ return acc;
406
+ },
407
+ {}
408
+ );
409
+ for (const domain of Object.keys(domains)) {
410
+ if (!domains[domain]) continue;
411
+ const result = await validateFormanValue(
412
+ domains[domain].values,
413
+ {
414
+ name: domain,
415
+ type: "collection",
416
+ spec: domains[domain].schema || []
417
+ },
418
+ {
419
+ roots,
420
+ domain,
421
+ path: [],
422
+ tail: [],
423
+ strict: options?.strict === true,
424
+ validateNestedFields: () => {
425
+ throw new Error("Cannot validate nested fields without parent field.");
426
+ },
427
+ resolveRemote: async (path, context) => {
428
+ if (!options?.resolveRemote) {
429
+ throw new Error("Remote resource not supported when resolver is not provided.");
430
+ }
431
+ const data = context.tail.reduce(
432
+ (acc, curr) => {
433
+ acc[curr.name] = curr.value;
434
+ return acc;
435
+ },
436
+ {}
437
+ );
438
+ return await options.resolveRemote(path, data);
439
+ }
440
+ }
441
+ );
442
+ errors.push(...result.errors);
443
+ }
444
+ return {
445
+ valid: errors.length === 0,
446
+ errors
447
+ };
448
+ }
449
+ async function validateFormanValue(value, field, context) {
450
+ if (FORMAN_VISUAL_TYPES.includes(field.type)) {
451
+ return {
452
+ valid: true,
453
+ errors: []
454
+ };
455
+ }
456
+ const normalizedField = normalizeFormanFieldType(field);
457
+ if (normalizedField.required && (value == null || value === "")) {
458
+ return {
459
+ valid: false,
460
+ errors: [
461
+ {
462
+ domain: context.domain,
463
+ path: context.path.join("."),
464
+ message: "Field is mandatory."
465
+ }
466
+ ]
467
+ };
468
+ }
469
+ if (value == null) {
470
+ return {
471
+ valid: true,
472
+ errors: []
473
+ };
474
+ }
475
+ const expectedType = FORMAN_TYPE_MAP2[normalizedField.type];
476
+ let actualType = typeof value;
477
+ if (actualType === "object" && Array.isArray(value)) actualType = "array";
478
+ if (expectedType && expectedType !== actualType) {
479
+ return {
480
+ valid: false,
481
+ errors: [
482
+ {
483
+ domain: context.domain,
484
+ path: context.path.join("."),
485
+ message: `Expected type '${expectedType}', got type '${actualType}'.`
486
+ }
487
+ ]
488
+ };
489
+ }
490
+ switch (normalizedField.type) {
491
+ case "collection":
492
+ return handleCollectionType2(value, normalizedField, context);
493
+ case "array":
494
+ return handleArrayType2(value, normalizedField, context);
495
+ case "select":
496
+ case "account":
497
+ case "hook":
498
+ case "keychain":
499
+ case "datastore":
500
+ case "aiagent":
501
+ case "udt":
502
+ case "file":
503
+ case "folder":
504
+ return handleSelectType2(value, normalizedField, context);
505
+ default:
506
+ return handlePrimitiveType2(value, normalizedField, context);
507
+ }
508
+ }
509
+ async function handleCollectionType2(value, field, context) {
510
+ const errors = [];
511
+ const seen = context.path.length === 0 ? context.roots[context.domain].seenFields : /* @__PURE__ */ new Set();
512
+ const path = context.path;
513
+ if (Array.isArray(field.spec)) {
514
+ for (const subField of field.spec) {
515
+ if (FORMAN_VISUAL_TYPES.includes(subField.type)) {
516
+ continue;
517
+ }
518
+ if (!subField.name) {
519
+ errors.push({
520
+ domain: context.domain,
521
+ path: context.path.join("."),
522
+ message: "Object contains field with unknown name."
523
+ });
524
+ continue;
525
+ }
526
+ if (context.strict && !seen.has(subField.name)) seen.add(subField.name);
527
+ const result = await validateFormanValue(value[subField.name], subField, {
528
+ ...context,
529
+ path: [...path, subField.name],
530
+ validateNestedFields: async (fields, context2) => {
531
+ for (const subField2 of fields) {
532
+ if (FORMAN_VISUAL_TYPES.includes(subField2.type)) {
533
+ continue;
534
+ }
535
+ if (!subField2.name) {
536
+ errors.push({
537
+ domain: context2.domain,
538
+ path: context2.path.join("."),
539
+ message: "Object contains field with unknown name."
540
+ });
541
+ continue;
542
+ }
543
+ if (context2.strict && !seen.has(subField2.name)) seen.add(subField2.name);
544
+ const result2 = await validateFormanValue(value[subField2.name], subField2, {
545
+ ...context2,
546
+ path: [...path, subField2.name]
547
+ });
548
+ errors.push(...result2.errors);
549
+ }
550
+ }
551
+ });
552
+ errors.push(...result.errors);
553
+ }
554
+ }
555
+ if (context.strict) {
556
+ for (const key of Object.keys(value)) {
557
+ if (!seen.has(key)) {
558
+ seen.add(key);
559
+ errors.push({
560
+ domain: context.domain,
561
+ path: context.path.join("."),
562
+ message: `Unknown field '${key}'.`
563
+ });
564
+ }
565
+ }
566
+ }
567
+ return {
568
+ valid: errors.length === 0,
569
+ errors
570
+ };
571
+ }
572
+ async function handleArrayType2(value, field, context) {
573
+ const errors = [];
574
+ if (field.spec) {
575
+ for (const [index, item] of value.entries()) {
576
+ const result = await validateFormanValue(
577
+ item,
578
+ Array.isArray(field.spec) ? { name: index.toString(), type: "collection", spec: field.spec } : Object.assign({}, field.spec, { name: index.toString() }),
579
+ { ...context, path: [...context.path, index.toString()] }
580
+ );
581
+ errors.push(...result.errors);
582
+ }
583
+ }
584
+ if (field.validate) {
585
+ if (field.validate.minItems !== void 0 && value.length < field.validate.minItems) {
586
+ errors.push({
587
+ domain: context.domain,
588
+ path: context.path.join("."),
589
+ message: `Array has less than ${field.validate.minItems} items.`
590
+ });
591
+ }
592
+ if (field.validate.maxItems !== void 0 && value.length > field.validate.maxItems) {
593
+ errors.push({
594
+ domain: context.domain,
595
+ path: context.path.join("."),
596
+ message: `Array has more than ${field.validate.maxItems} items.`
597
+ });
598
+ }
599
+ }
600
+ return {
601
+ valid: errors.length === 0,
602
+ errors
603
+ };
604
+ }
605
+ async function handleSelectType2(value, field, context) {
606
+ const errors = [];
607
+ let optionsOrGroups = isObject(field.options) ? field.options.store : field.options;
608
+ let nested = isObject(field.options) ? field.options.nested : void 0;
609
+ if (typeof optionsOrGroups === "string") {
610
+ try {
611
+ optionsOrGroups = await context.resolveRemote(optionsOrGroups, context);
612
+ } catch (error) {
613
+ return {
614
+ valid: false,
615
+ errors: [
616
+ ...errors,
617
+ {
618
+ domain: context.domain,
619
+ path: context.path.join("."),
620
+ message: `Failed to resolve remote resource ${optionsOrGroups}: ${error}`
621
+ }
622
+ ]
623
+ };
624
+ }
625
+ }
626
+ if (field.multiple) {
627
+ if (!Array.isArray(value)) {
628
+ return {
629
+ valid: false,
630
+ errors: [
631
+ ...errors,
632
+ {
633
+ domain: context.domain,
634
+ path: context.path.join("."),
635
+ message: `Value is not an array.`
636
+ }
637
+ ]
638
+ };
639
+ }
640
+ for (const singleValue of value) {
641
+ const found = field.grouped ? optionsOrGroups.some(
642
+ (group) => group.options.some((option) => option.value === singleValue)
643
+ ) : optionsOrGroups.some((option) => option.value === singleValue);
644
+ if (!found) {
645
+ errors.push({
646
+ domain: context.domain,
647
+ path: context.path.join("."),
648
+ message: `Value '${singleValue}' not found in options.`
649
+ });
650
+ }
651
+ }
652
+ if (field.validate) {
653
+ if (field.validate.minItems !== void 0 && value.length < field.validate.minItems) {
654
+ errors.push({
655
+ domain: context.domain,
656
+ path: context.path.join("."),
657
+ message: `Selected less than ${field.validate.minItems} items.`
658
+ });
659
+ }
660
+ if (field.validate.maxItems !== void 0 && value.length > field.validate.maxItems) {
661
+ errors.push({
662
+ domain: context.domain,
663
+ path: context.path.join("."),
664
+ message: `Selected more than ${field.validate.maxItems} items.`
665
+ });
666
+ }
667
+ }
668
+ } else {
669
+ const item = field.grouped ? optionsOrGroups.find((group) => group.options.some((option) => option.value === value))?.options.find((option) => option.value === value) : optionsOrGroups.find((option) => option.value === value);
670
+ if (!item) {
671
+ return {
672
+ valid: false,
673
+ errors: [
674
+ ...errors,
675
+ {
676
+ domain: context.domain,
677
+ path: context.path.join("."),
678
+ message: `Value '${value}' not found in options.`
679
+ }
680
+ ]
681
+ };
682
+ }
683
+ if (item.nested) nested = item.nested;
684
+ }
685
+ if (nested) {
686
+ const result = await handleNestedFields(nested, value, field, context);
687
+ errors.push(...result.errors);
688
+ }
689
+ return {
690
+ valid: errors.length === 0,
691
+ errors
692
+ };
693
+ }
694
+ async function handleNestedFields(nested, value, field, context) {
695
+ const errors = [];
696
+ let store = isObject(nested) ? nested.store : nested;
697
+ const domain = isObject(nested) && nested.domain ? nested.domain : void 0;
698
+ context = {
699
+ ...context,
700
+ tail: field.name ? [...context.tail, { name: field.name, value }] : context.tail
701
+ };
702
+ if (typeof store === "string") {
703
+ try {
704
+ store = await context.resolveRemote(store, context);
705
+ } catch (error) {
706
+ return {
707
+ valid: false,
708
+ errors: [
709
+ ...errors,
710
+ {
711
+ domain: context.domain,
712
+ path: context.path.join("."),
713
+ message: `Failed to resolve remote resource ${store}: ${error}`
714
+ }
715
+ ]
716
+ };
717
+ }
718
+ }
719
+ if (store && domain && domain !== context.domain) {
720
+ if (!context.roots[domain]) {
721
+ errors.push({
722
+ domain: context.domain,
723
+ path: context.path.join("."),
724
+ message: `Unable to process nested fields: Domain '${domain}' not found.`
725
+ });
726
+ } else {
727
+ const result = await context.roots[domain].validateFields(store, context);
728
+ errors.push(...result.errors);
729
+ }
730
+ } else if (store) {
731
+ await context.validateNestedFields(store, context);
732
+ }
733
+ return {
734
+ valid: errors.length === 0,
735
+ errors
736
+ };
737
+ }
738
+ async function handlePrimitiveType2(value, field, context) {
739
+ const errors = [];
740
+ if (containsIMLExpression(value)) {
741
+ return {
742
+ valid: true,
743
+ errors
744
+ };
745
+ }
746
+ if (errors.length > 0) {
747
+ return {
748
+ valid: false,
749
+ errors
750
+ };
751
+ }
752
+ if (field.validate) {
753
+ if (typeof value === "string") {
754
+ if (field.validate.pattern && !new RegExp(
755
+ typeof field.validate.pattern === "object" ? field.validate.pattern.regexp : field.validate.pattern
756
+ ).test(value)) {
757
+ errors.push({
758
+ domain: context.domain,
759
+ path: context.path.join("."),
760
+ message: `Value doesn't match the pattern: ${typeof field.validate.pattern === "object" ? field.validate.pattern.regexp : field.validate.pattern}`
761
+ });
762
+ }
763
+ if (field.validate.min !== void 0 && value.length < field.validate.min) {
764
+ errors.push({
765
+ domain: context.domain,
766
+ path: context.path.join("."),
767
+ message: `Value must be at least ${field.validate.min} characters long.`
768
+ });
769
+ }
770
+ if (field.validate.max !== void 0 && value.length > field.validate.max) {
771
+ errors.push({
772
+ domain: context.domain,
773
+ path: context.path.join("."),
774
+ message: `Value exceeded maximum length of ${field.validate.max} characters.`
775
+ });
776
+ }
777
+ if (field.validate.enum && !field.validate.enum.includes(value)) {
778
+ errors.push({
779
+ domain: context.domain,
780
+ path: context.path.join("."),
781
+ message: "Value must be one of the following: " + field.validate.enum.join(", ")
782
+ });
783
+ }
784
+ } else if (typeof value === "number") {
785
+ if (field.validate.min !== void 0 && value < field.validate.min) {
786
+ errors.push({
787
+ domain: context.domain,
788
+ path: context.path.join("."),
789
+ message: `Value is too small. Minimum value is ${field.validate.min}.`
790
+ });
791
+ }
792
+ if (field.validate.max !== void 0 && value > field.validate.max) {
793
+ errors.push({
794
+ domain: context.domain,
795
+ path: context.path.join("."),
796
+ message: `Value is too big. Maximum value is ${field.validate.max}.`
797
+ });
798
+ }
799
+ }
800
+ }
801
+ if (field.nested) {
802
+ const result = await handleNestedFields(field.nested, value, field, context);
803
+ errors.push(...result.errors);
804
+ }
805
+ return {
806
+ valid: errors.length === 0,
807
+ errors
808
+ };
809
+ }
810
+
348
811
  // src/json.ts
349
812
  var JSON_PRIMITIVE_TYPE_MAP = {
350
813
  string: "text",
@@ -434,8 +897,16 @@ function toFormanSchema(field) {
434
897
  function toJSONSchema(field) {
435
898
  return toJSONSchemaInternal(field);
436
899
  }
900
+ function validateFormanWithDomains(domains, options) {
901
+ return validateFormanWithDomainsInternal(domains, options);
902
+ }
903
+ function validateForman(values, schema, options) {
904
+ return validateFormanWithDomains({ default: { values, schema } }, options);
905
+ }
437
906
  // Annotate the CommonJS export names for ESM import in node:
438
907
  0 && (module.exports = {
439
908
  toFormanSchema,
440
- toJSONSchema
909
+ toJSONSchema,
910
+ validateForman,
911
+ validateFormanWithDomains
441
912
  });