@revisium/schema-toolkit 0.11.0 → 0.12.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.
@@ -33,6 +33,15 @@ var xFormulaRequiresReadOnly = {
33
33
  required: ["readOnly"]
34
34
  }
35
35
  };
36
+ var foreignKeyExcludesFormula = {
37
+ if: {
38
+ properties: { foreignKey: { type: "string" } },
39
+ required: ["foreignKey"]
40
+ },
41
+ then: {
42
+ not: { required: ["x-formula"] }
43
+ }
44
+ };
36
45
  var refMetaSchema = {
37
46
  type: "object",
38
47
  properties: {
@@ -92,7 +101,7 @@ var stringMetaSchema = {
92
101
  },
93
102
  additionalProperties: false,
94
103
  required: ["type", "default"],
95
- ...xFormulaRequiresReadOnly
104
+ allOf: [xFormulaRequiresReadOnly, foreignKeyExcludesFormula]
96
105
  };
97
106
  var noForeignKeyStringMetaSchema = {
98
107
  type: "object",
@@ -391,6 +400,7 @@ var tableMigrationsSchema = {
391
400
  exports.arrayMetaSchema = arrayMetaSchema;
392
401
  exports.baseStringFields = baseStringFields;
393
402
  exports.booleanMetaSchema = booleanMetaSchema;
403
+ exports.foreignKeyExcludesFormula = foreignKeyExcludesFormula;
394
404
  exports.historyPatchesSchema = historyPatchesSchema;
395
405
  exports.jsonPatchSchema = jsonPatchSchema;
396
406
  exports.metaSchema = metaSchema;
@@ -404,5 +414,5 @@ exports.stringMetaSchema = stringMetaSchema;
404
414
  exports.tableMigrationsSchema = tableMigrationsSchema;
405
415
  exports.xFormulaRequiresReadOnly = xFormulaRequiresReadOnly;
406
416
  exports.xFormulaSchema = xFormulaSchema;
407
- //# sourceMappingURL=chunk-T7VRK5OQ.cjs.map
408
- //# sourceMappingURL=chunk-T7VRK5OQ.cjs.map
417
+ //# sourceMappingURL=chunk-32FZHFIO.cjs.map
418
+ //# sourceMappingURL=chunk-32FZHFIO.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/validation-schemas/shared-fields.ts","../src/validation-schemas/meta-schema.ts","../src/validation-schemas/json-patch-schema.ts","../src/validation-schemas/history-patches-schema.ts","../src/validation-schemas/table-migrations-schema.ts"],"names":[],"mappings":";;;AAAO,IAAM,YAAA,GAAe;AAAA,EAC1B,UAAA,EAAY;AAAA,IACV,IAAA,EAAM;AAAA,GACR;AAAA,EACA,KAAA,EAAO;AAAA,IACL,IAAA,EAAM;AAAA,GACR;AAAA,EACA,WAAA,EAAa;AAAA,IACX,IAAA,EAAM;AAAA;AAEV;;;ACLO,IAAM,cAAA,GAAyB;AAAA,EACpC,IAAA,EAAM,QAAA;AAAA,EACN,UAAA,EAAY;AAAA,IACV,OAAA,EAAS,EAAE,KAAA,EAAO,CAAA,EAAE;AAAA,IACpB,YAAY,EAAE,IAAA,EAAM,UAAU,SAAA,EAAW,CAAA,EAAG,WAAW,GAAA;AAAM,GAC/D;AAAA,EACA,QAAA,EAAU,CAAC,SAAA,EAAW,YAAY,CAAA;AAAA,EAClC,oBAAA,EAAsB;AACxB;AAGO,IAAM,wBAAA,GAAmC;AAAA,EAC9C,EAAA,EAAI;AAAA,IACF,YAAY,EAAE,WAAA,EAAa,EAAE,IAAA,EAAM,UAAS,EAAE;AAAA,IAC9C,QAAA,EAAU,CAAC,WAAW;AAAA,GACxB;AAAA,EACA,IAAA,EAAM;AAAA,IACJ,YAAY,EAAE,QAAA,EAAU,EAAE,KAAA,EAAO,MAAK,EAAE;AAAA,IACxC,QAAA,EAAU,CAAC,UAAU;AAAA;AAEzB;AAGO,IAAM,yBAAA,GAAoC;AAAA,EAC/C,EAAA,EAAI;AAAA,IACF,YAAY,EAAE,UAAA,EAAY,EAAE,IAAA,EAAM,UAAS,EAAE;AAAA,IAC7C,QAAA,EAAU,CAAC,YAAY;AAAA,GACzB;AAAA,EACA,IAAA,EAAM;AAAA,IACJ,GAAA,EAAK,EAAE,QAAA,EAAU,CAAC,WAAW,CAAA;AAAE;AAEnC;AAEO,IAAM,aAAA,GAAwB;AAAA,EACnC,IAAA,EA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EAAS,EAAE,IAAA,EAAM,QAAA,EAAS;AAAA,QAC1B,WAAA,EAAa,EAAE,IAAA,EAAM,QAAA;AAAS;AAChC,KACF;AAAA,IACA,eAAA,EAAiB;AAAA,MACf,IAAA,EAAM,QAAA;AAAA,MACN,oBAAA,EAAsB,KAAA;AAAA,MACtB,QAAA,EAAU,CAAC,YAAA,EAAc,IAAA,EAAM,SAAS,CAAA;AAAA,MACxC,UAAA,EAAY;AAAA,QACV,UAAA,EAAY,EAAE,IAAA,EAAM,QAAA,EAAU,OAAO,QAAA,EAAS;AAAA,QAC9C,EAAA,EAAI,EAAE,IAAA,EAAM,QAAA,EAAS;AAAA,QACrB,OAAA,EAAS,EAAE,IAAA,EAAM,QAAA;AAAS;AAC5B;AACF,GACF;AAAA,EACA,KAAA,EAAO;AACT","file":"chunk-32FZHFIO.cjs","sourcesContent":["export const sharedFields = {\n deprecated: {\n type: 'boolean',\n },\n title: {\n type: 'string',\n },\n description: {\n type: 'string',\n },\n};\n","import { Schema } from 'ajv/dist/2020';\nimport { sharedFields } from './shared-fields.js';\n\n// https://json-schema.org/specification#single-vocabulary-meta-schemas\n\nexport const xFormulaSchema: Schema = {\n type: 'object',\n properties: {\n version: { const: 1 },\n expression: { type: 'string', minLength: 1, maxLength: 10000 },\n },\n required: ['version', 'expression'],\n additionalProperties: false,\n};\n\n// When x-formula is present, readOnly must be true\nexport const xFormulaRequiresReadOnly: Schema = {\n if: {\n properties: { 'x-formula': { type: 'object' } },\n required: ['x-formula'],\n },\n then: {\n properties: { readOnly: { const: true } },\n required: ['readOnly'],\n },\n};\n\n// foreignKey and x-formula are mutually exclusive\nexport const foreignKeyExcludesFormula: Schema = {\n if: {\n properties: { foreignKey: { type: 'string' } },\n required: ['foreignKey'],\n },\n then: {\n not: { required: ['x-formula'] },\n },\n};\n\nexport const refMetaSchema: Schema = {\n type: 'object',\n properties: {\n ...sharedFields,\n $ref: {\n type: 'string',\n },\n },\n additionalProperties: false,\n required: ['$ref'],\n};\n\nexport const baseStringFields: Schema = {\n type: {\n const: 'string',\n },\n default: {\n type: 'string',\n },\n readOnly: {\n type: 'boolean',\n },\n pattern: {\n type: 'string',\n format: 'regex',\n },\n enum: {\n type: 'array',\n items: { type: 'string' },\n minItems: 1,\n uniqueItems: true,\n },\n format: {\n type: 'string',\n enum: ['date-time', 'date', 'time', 'email', 'regex'],\n },\n contentMediaType: {\n type: 'string',\n enum: [\n 'text/plain',\n 'text/markdown',\n 'text/html',\n 'application/json',\n 'application/schema+json',\n 'application/yaml',\n ],\n },\n 'x-formula': xFormulaSchema,\n ...sharedFields,\n};\n\nexport const stringMetaSchema: Schema = {\n type: 'object',\n properties: {\n ...baseStringFields,\n foreignKey: {\n type: 'string',\n },\n },\n additionalProperties: false,\n required: ['type', 'default'],\n allOf: [xFormulaRequiresReadOnly, foreignKeyExcludesFormula],\n};\n\nexport const noForeignKeyStringMetaSchema: Schema = {\n type: 'object',\n properties: {\n ...baseStringFields,\n },\n additionalProperties: false,\n required: ['type', 'default'],\n ...xFormulaRequiresReadOnly,\n};\n\nexport const numberMetaSchema: Schema = {\n type: 'object',\n properties: {\n type: {\n const: 'number',\n },\n default: {\n type: 'number',\n },\n readOnly: {\n type: 'boolean',\n },\n 'x-formula': xFormulaSchema,\n ...sharedFields,\n },\n additionalProperties: false,\n required: ['type', 'default'],\n ...xFormulaRequiresReadOnly,\n};\n\nexport const booleanMetaSchema: Schema = {\n type: 'object',\n properties: {\n type: {\n const: 'boolean',\n },\n default: {\n type: 'boolean',\n },\n readOnly: {\n type: 'boolean',\n },\n 'x-formula': xFormulaSchema,\n ...sharedFields,\n },\n additionalProperties: false,\n required: ['type', 'default'],\n ...xFormulaRequiresReadOnly,\n};\n\nexport const objectMetaSchema: Schema = {\n type: 'object',\n properties: {\n type: {\n const: 'object',\n },\n ...sharedFields,\n properties: {\n type: 'object',\n additionalProperties: { $dynamicRef: '#meta' },\n default: {},\n },\n additionalProperties: { const: false },\n required: { $ref: '#/$defs/stringArray' },\n },\n additionalProperties: false,\n required: ['type', 'properties', 'additionalProperties', 'required'],\n};\n\nexport const arrayMetaSchema: Schema = {\n type: 'object',\n properties: {\n type: {\n const: 'array',\n },\n ...sharedFields,\n items: {\n oneOf: [\n { $ref: '#/$defs/refSchema' },\n { $ref: '#/$defs/objectSchema' },\n { $ref: '#/$defs/arraySchema' },\n { $ref: '#/$defs/stringSchema' },\n { $ref: '#/$defs/numberSchema' },\n { $ref: '#/$defs/booleanSchema' },\n ],\n },\n },\n additionalProperties: false,\n required: ['type', 'items'],\n};\n\nexport const metaSchema: Schema = {\n $id: 'meta-schema.json',\n type: 'object',\n $dynamicAnchor: 'meta',\n oneOf: [\n { $ref: '#/$defs/refSchema' },\n { $ref: '#/$defs/objectSchema' },\n { $ref: '#/$defs/arraySchema' },\n { $ref: '#/$defs/stringSchema' },\n { $ref: '#/$defs/numberSchema' },\n { $ref: '#/$defs/booleanSchema' },\n ],\n $defs: {\n stringArray: {\n type: 'array',\n items: { type: 'string' },\n uniqueItems: true,\n default: [],\n },\n refSchema: refMetaSchema,\n objectSchema: objectMetaSchema,\n stringSchema: stringMetaSchema,\n numberSchema: numberMetaSchema,\n booleanSchema: booleanMetaSchema,\n arraySchema: arrayMetaSchema,\n },\n};\n\nexport const notForeignKeyMetaSchema: Schema = {\n type: 'object',\n $dynamicAnchor: 'meta',\n oneOf: [\n { $ref: '#/$defs/refSchema' },\n { $ref: '#/$defs/objectSchema' },\n { $ref: '#/$defs/arraySchema' },\n { $ref: '#/$defs/stringSchema' },\n { $ref: '#/$defs/numberSchema' },\n { $ref: '#/$defs/booleanSchema' },\n ],\n $defs: {\n stringArray: {\n type: 'array',\n items: { type: 'string' },\n uniqueItems: true,\n default: [],\n },\n refSchema: refMetaSchema,\n objectSchema: objectMetaSchema,\n stringSchema: noForeignKeyStringMetaSchema,\n numberSchema: numberMetaSchema,\n booleanSchema: booleanMetaSchema,\n arraySchema: arrayMetaSchema,\n },\n};\n","import { Schema } from 'ajv/dist/2020';\nimport {\n arrayMetaSchema,\n booleanMetaSchema,\n numberMetaSchema,\n objectMetaSchema,\n stringMetaSchema,\n} from './meta-schema.js';\n\n// copied and modified from https://json.schemastore.org/json-patch\n\nexport const jsonPatchSchema: Schema = {\n $id: 'json-patch-schema.json',\n definitions: {\n path: {\n description: 'A JSON Pointer path.',\n type: 'string',\n },\n objectSchema: objectMetaSchema,\n stringSchema: stringMetaSchema,\n numberSchema: numberMetaSchema,\n booleanSchema: booleanMetaSchema,\n arraySchema: arrayMetaSchema,\n },\n minItems: 1,\n items: {\n oneOf: [\n {\n type: 'object',\n additionalProperties: false,\n required: ['value', 'op', 'path'],\n properties: {\n path: {\n $ref: '#/definitions/path',\n },\n op: {\n description: 'The operation to perform.',\n type: 'string',\n enum: ['add', 'replace'],\n },\n value: {\n $ref: 'meta-schema.json',\n description: 'The value to add, replace or test.',\n },\n },\n },\n {\n type: 'object',\n additionalProperties: false,\n required: ['op', 'path'],\n properties: {\n path: {\n $ref: '#/definitions/path',\n },\n op: {\n description: 'The operation to perform.',\n type: 'string',\n enum: ['remove'],\n },\n },\n },\n {\n type: 'object',\n additionalProperties: false,\n required: ['from', 'op', 'path'],\n properties: {\n path: {\n $ref: '#/definitions/path',\n },\n op: {\n description: 'The operation to perform.',\n type: 'string',\n enum: ['move', 'copy'],\n },\n from: {\n $ref: '#/definitions/path',\n description:\n 'A JSON Pointer path pointing to the location to move/copy from.',\n },\n },\n },\n ],\n },\n title: 'JSON schema for JSONPatch files',\n type: 'array',\n};\n","import { Schema } from 'ajv/dist/2020';\n\nexport const historyPatchesSchema: Schema = {\n $id: 'history-patches-schema.json',\n type: 'array',\n minItems: 1,\n items: {\n type: 'object',\n properties: {\n patches: {\n $ref: 'json-patch-schema.json',\n },\n hash: {\n type: 'string',\n },\n },\n required: ['patches', 'hash'],\n },\n};\n","import { Schema } from 'ajv/dist/2020';\n\nexport const tableMigrationsSchema: Schema = {\n $id: 'table-migrations-schema.json',\n oneOf: [\n { $ref: '#/definitions/InitMigration' },\n { $ref: '#/definitions/UpdateMigration' },\n { $ref: '#/definitions/RenameMigration' },\n { $ref: '#/definitions/RemoveMigration' },\n ],\n definitions: {\n InitMigration: {\n type: 'object',\n additionalProperties: false,\n required: ['changeType', 'tableId', 'hash', 'id', 'schema'],\n properties: {\n changeType: { type: 'string', const: 'init' },\n tableId: { type: 'string' },\n hash: { type: 'string' },\n id: { type: 'string' },\n schema: { $ref: 'meta-schema.json' },\n },\n },\n UpdateMigration: {\n type: 'object',\n additionalProperties: false,\n required: ['changeType', 'tableId', 'hash', 'id', 'patches'],\n properties: {\n changeType: { type: 'string', const: 'update' },\n tableId: { type: 'string' },\n hash: { type: 'string' },\n id: { type: 'string' },\n patches: { $ref: 'json-patch-schema.json' },\n },\n },\n RenameMigration: {\n type: 'object',\n additionalProperties: false,\n required: ['changeType', 'id', 'tableId', 'nextTableId'],\n properties: {\n changeType: { type: 'string', const: 'rename' },\n id: { type: 'string' },\n tableId: { type: 'string' },\n nextTableId: { type: 'string' },\n },\n },\n RemoveMigration: {\n type: 'object',\n additionalProperties: false,\n required: ['changeType', 'id', 'tableId'],\n properties: {\n changeType: { type: 'string', const: 'remove' },\n id: { type: 'string' },\n tableId: { type: 'string' },\n },\n },\n },\n title: 'JSON Schema for a Single Migration',\n};\n"]}
@@ -2,6 +2,8 @@
2
2
 
3
3
  var chunkEGC32GPY_cjs = require('./chunk-EGC32GPY.cjs');
4
4
  var chunkTMWPWGUN_cjs = require('./chunk-TMWPWGUN.cjs');
5
+ var formula = require('@revisium/formula');
6
+ var spec = require('@revisium/formula/spec');
5
7
 
6
8
  // src/lib/createJsonSchemaStore.ts
7
9
  var createJsonSchemaStore = (schema, refs = {}) => {
@@ -571,6 +573,472 @@ function createChange(path, oldValue, newValue, changeType) {
571
573
  return { path, oldValue, newValue, changeType };
572
574
  }
573
575
 
576
+ // src/lib/extract-schema-formulas.ts
577
+ function extractSchemaFormulas(schema) {
578
+ const formulas = [];
579
+ extractFormulasRecursive(schema, "", formulas);
580
+ return formulas;
581
+ }
582
+ function extractFormulasRecursive(schema, pathPrefix, formulas) {
583
+ if (schema.type === "array" && schema.items) {
584
+ extractFormulasRecursive(schema.items, `${pathPrefix}[]`, formulas);
585
+ return;
586
+ }
587
+ const properties = schema.properties ?? {};
588
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
589
+ const fullPath = pathPrefix ? `${pathPrefix}.${fieldName}` : fieldName;
590
+ const xFormula = fieldSchema["x-formula"];
591
+ if (xFormula) {
592
+ formulas.push({
593
+ fieldName: fullPath,
594
+ expression: xFormula.expression,
595
+ fieldType: fieldSchema.type ?? "string"
596
+ });
597
+ }
598
+ if (fieldSchema.type === "object" && fieldSchema.properties) {
599
+ extractFormulasRecursive(fieldSchema, fullPath, formulas);
600
+ }
601
+ if (fieldSchema.type === "array" && fieldSchema.items) {
602
+ extractFormulasRecursive(fieldSchema.items, `${fullPath}[]`, formulas);
603
+ }
604
+ }
605
+ }
606
+ function validateSchemaFormulas(schema) {
607
+ const errors = [];
608
+ const formulas = extractSchemaFormulas(schema);
609
+ for (const formula of formulas) {
610
+ const error = validateFormulaAgainstSchema(
611
+ formula.expression,
612
+ formula.fieldName,
613
+ schema
614
+ );
615
+ if (error) {
616
+ errors.push(error);
617
+ }
618
+ }
619
+ if (errors.length > 0) {
620
+ return { isValid: false, errors };
621
+ }
622
+ const dependencies = {};
623
+ for (const formula$1 of formulas) {
624
+ const parseResult = formula.parseExpression(formula$1.expression);
625
+ const parentPath = getParentPath(formula$1.fieldName);
626
+ const prefix = parentPath ? `${parentPath}.` : "";
627
+ dependencies[formula$1.fieldName] = parseResult.dependencies.map((dep) => {
628
+ if (dep.startsWith("/")) {
629
+ return extractFieldRoot(dep.slice(1));
630
+ }
631
+ const rootField = extractFieldRoot(dep);
632
+ return `${prefix}${rootField}`;
633
+ });
634
+ }
635
+ const graph = formula.buildDependencyGraph(dependencies);
636
+ const circularCheck = formula.detectCircularDependencies(graph);
637
+ const cycle = circularCheck.cycle;
638
+ if (circularCheck.hasCircular && cycle && cycle.length > 0) {
639
+ const firstField = cycle[0];
640
+ if (firstField) {
641
+ errors.push({
642
+ field: firstField,
643
+ error: `Circular dependency: ${cycle.join(" \u2192 ")}`
644
+ });
645
+ return { isValid: false, errors };
646
+ }
647
+ }
648
+ return { isValid: true, errors: [] };
649
+ }
650
+ function validateFormulaAgainstSchema(expression, fieldName, schema) {
651
+ return validateFormulaInContext(expression, fieldName, schema);
652
+ }
653
+ function validateFormulaInContext(expression, fieldPath, rootSchema) {
654
+ const syntaxResult = formula.validateFormulaSyntax(expression);
655
+ if (!syntaxResult.isValid) {
656
+ return {
657
+ field: fieldPath,
658
+ error: syntaxResult.error,
659
+ position: syntaxResult.position
660
+ };
661
+ }
662
+ const parentPath = getParentPath(fieldPath);
663
+ const localFieldName = getFieldName(fieldPath);
664
+ const contextSchema = resolveSubSchema(rootSchema, parentPath);
665
+ if (!contextSchema) {
666
+ return {
667
+ field: fieldPath,
668
+ error: `Cannot resolve schema context for path '${parentPath}'`
669
+ };
670
+ }
671
+ const parseResult = formula.parseExpression(expression);
672
+ const localSchemaFields = getSchemaFields(contextSchema);
673
+ const rootSchemaFields = getSchemaFields(rootSchema);
674
+ for (const dep of parseResult.dependencies) {
675
+ if (dep.startsWith("/")) {
676
+ const rootField = extractFieldRoot(dep.slice(1));
677
+ if (!rootSchemaFields.has(rootField)) {
678
+ return {
679
+ field: fieldPath,
680
+ error: `Unknown root field '${rootField}' in formula`
681
+ };
682
+ }
683
+ } else {
684
+ const rootField = extractFieldRoot(dep);
685
+ if (!localSchemaFields.has(rootField)) {
686
+ return {
687
+ field: fieldPath,
688
+ error: `Unknown field '${rootField}' in formula`
689
+ };
690
+ }
691
+ }
692
+ }
693
+ if (parseResult.dependencies.some((d) => extractFieldRoot(d) === localFieldName)) {
694
+ return {
695
+ field: fieldPath,
696
+ error: `Formula cannot reference itself`
697
+ };
698
+ }
699
+ const fieldSchema = contextSchema.properties?.[localFieldName];
700
+ const expectedType = schemaTypeToInferred(fieldSchema?.type);
701
+ const fieldTypes = getSchemaFieldTypes(contextSchema);
702
+ const inferredType = formula.inferFormulaType(expression, fieldTypes);
703
+ if (!isTypeCompatible(inferredType, expectedType)) {
704
+ return {
705
+ field: fieldPath,
706
+ error: `Type mismatch: formula returns '${inferredType}' but field expects '${expectedType}'`
707
+ };
708
+ }
709
+ return null;
710
+ }
711
+ function resolveSubSchema(schema, fieldPath) {
712
+ if (!fieldPath) {
713
+ return schema;
714
+ }
715
+ const segments = parsePathSegments2(fieldPath);
716
+ let current = schema;
717
+ for (const segment of segments) {
718
+ if (segment === "[]") {
719
+ if (current.type === "array" && current.items) {
720
+ current = current.items;
721
+ } else {
722
+ return null;
723
+ }
724
+ } else if (current.properties?.[segment]) {
725
+ current = current.properties[segment];
726
+ } else {
727
+ return null;
728
+ }
729
+ }
730
+ return current;
731
+ }
732
+ function parsePathSegments2(path) {
733
+ const segments = [];
734
+ let current = "";
735
+ let inBracket = false;
736
+ for (const char of path) {
737
+ if (char === "[") {
738
+ if (current) {
739
+ segments.push(current);
740
+ current = "";
741
+ }
742
+ inBracket = true;
743
+ } else if (char === "]") {
744
+ inBracket = false;
745
+ segments.push("[]");
746
+ } else if (char === "." && !inBracket) {
747
+ if (current) {
748
+ segments.push(current);
749
+ current = "";
750
+ }
751
+ } else if (!inBracket) {
752
+ current += char;
753
+ }
754
+ }
755
+ if (current) {
756
+ segments.push(current);
757
+ }
758
+ return segments;
759
+ }
760
+ function getParentPath(fieldPath) {
761
+ const lastDotIndex = fieldPath.lastIndexOf(".");
762
+ const lastBracketIndex = fieldPath.lastIndexOf("[");
763
+ const splitIndex = Math.max(lastDotIndex, lastBracketIndex);
764
+ if (splitIndex <= 0) {
765
+ return "";
766
+ }
767
+ return fieldPath.substring(0, splitIndex);
768
+ }
769
+ function getFieldName(fieldPath) {
770
+ const lastDotIndex = fieldPath.lastIndexOf(".");
771
+ const lastBracketIndex = fieldPath.lastIndexOf("]");
772
+ const splitIndex = Math.max(lastDotIndex, lastBracketIndex);
773
+ if (splitIndex === -1) {
774
+ return fieldPath;
775
+ }
776
+ return fieldPath.substring(splitIndex + 1);
777
+ }
778
+ function getSchemaFields(schema) {
779
+ const fields = /* @__PURE__ */ new Set();
780
+ const properties = schema.properties ?? {};
781
+ for (const fieldName of Object.keys(properties)) {
782
+ fields.add(fieldName);
783
+ }
784
+ return fields;
785
+ }
786
+ function getSchemaFieldTypes(schema) {
787
+ const fieldTypes = {};
788
+ const properties = schema.properties ?? {};
789
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
790
+ const schemaType = fieldSchema.type;
791
+ if (schemaType === "integer") {
792
+ fieldTypes[fieldName] = "number";
793
+ } else if (schemaType === "number" || schemaType === "string" || schemaType === "boolean" || schemaType === "object" || schemaType === "array") {
794
+ fieldTypes[fieldName] = schemaType;
795
+ }
796
+ }
797
+ return fieldTypes;
798
+ }
799
+ function schemaTypeToInferred(schemaType) {
800
+ if (schemaType === "number" || schemaType === "integer") return "number";
801
+ if (schemaType === "string") return "string";
802
+ if (schemaType === "boolean") return "boolean";
803
+ return null;
804
+ }
805
+ function isTypeCompatible(inferredType, expectedType) {
806
+ if (expectedType === null) return true;
807
+ if (inferredType === "unknown") return true;
808
+ return inferredType === expectedType;
809
+ }
810
+ function extractFieldRoot(dependency) {
811
+ const root = dependency.split(".")[0]?.split("[")[0];
812
+ return root || dependency;
813
+ }
814
+
815
+ // src/lib/formula.ts
816
+ function prepareFormulas(schema) {
817
+ const formulas = extractSchemaFormulas(schema);
818
+ if (formulas.length === 0) {
819
+ return [];
820
+ }
821
+ const formulasWithMeta = formulas.map((f) => enrichFormula(f));
822
+ if (formulas.length <= 1) {
823
+ return formulasWithMeta;
824
+ }
825
+ return orderByDependencies(formulasWithMeta);
826
+ }
827
+ function evaluateFormulas(formulas, data, options = {}) {
828
+ const values = {};
829
+ const errors = [];
830
+ const failedFields = /* @__PURE__ */ new Set();
831
+ for (const formula of formulas) {
832
+ const hasDependencyFailure = formula.dependencies.some(
833
+ (dep) => failedFields.has(dep)
834
+ );
835
+ if (hasDependencyFailure) {
836
+ failedFields.add(formula.fieldName);
837
+ if (options.useDefaults) {
838
+ const defaultValue = getDefaultValue(formula, options.defaults);
839
+ setValueByPath2(values, formula.fieldName, defaultValue);
840
+ setValueByPath2(data, formula.fieldName, defaultValue);
841
+ }
842
+ errors.push(
843
+ createError(formula, "Dependency formula failed", options.useDefaults ?? false)
844
+ );
845
+ continue;
846
+ }
847
+ const formulaErrors = evaluateFormula(formula, data, values, options);
848
+ if (formulaErrors.length > 0) {
849
+ errors.push(...formulaErrors);
850
+ failedFields.add(formula.fieldName);
851
+ }
852
+ }
853
+ return { values, errors };
854
+ }
855
+ function enrichFormula(formula) {
856
+ const dependencies = parseDependencies(formula.expression);
857
+ const pathInfo = parseArrayItemPath(formula.fieldName);
858
+ return { ...formula, dependencies, ...pathInfo };
859
+ }
860
+ function parseDependencies(expression) {
861
+ try {
862
+ return formula.parseFormula(expression).dependencies;
863
+ } catch {
864
+ return [];
865
+ }
866
+ }
867
+ function parseArrayItemPath(fieldName) {
868
+ const bracketIndex = fieldName.indexOf("[]");
869
+ if (bracketIndex === -1) {
870
+ return { isArrayItem: false, arrayPath: null, localFieldPath: fieldName };
871
+ }
872
+ const arrayPath = fieldName.slice(0, bracketIndex);
873
+ const afterBrackets = fieldName.slice(bracketIndex + 2);
874
+ const localFieldPath = afterBrackets.startsWith(".") ? afterBrackets.slice(1) : afterBrackets;
875
+ return { isArrayItem: true, arrayPath, localFieldPath };
876
+ }
877
+ function orderByDependencies(formulas) {
878
+ const dependencies = Object.fromEntries(
879
+ formulas.map((f) => [f.fieldName, f.dependencies])
880
+ );
881
+ const result = formula.getTopologicalOrder(formula.buildDependencyGraph(dependencies));
882
+ if (!result.success) {
883
+ throw new Error(
884
+ `Cyclic dependency detected in formulas: ${result.error ?? "unknown error"}`
885
+ );
886
+ }
887
+ const formulaMap = new Map(formulas.map((f) => [f.fieldName, f]));
888
+ return result.order.map((name) => formulaMap.get(name)).filter((f) => f !== void 0);
889
+ }
890
+ function evaluateFormula(formula, data, values, options) {
891
+ if (formula.isArrayItem && formula.arrayPath) {
892
+ return evaluateArrayFormula(formula, data, values, options);
893
+ }
894
+ return evaluateSingleFormula(formula, data, values, options);
895
+ }
896
+ function evaluateSingleFormula(formula$1, data, values, options) {
897
+ const context = { rootData: data };
898
+ try {
899
+ const result = formula.evaluateWithContext(formula$1.expression, context);
900
+ if (result === void 0) {
901
+ if (options.useDefaults) {
902
+ const defaultValue = getDefaultValue(formula$1, options.defaults);
903
+ setValueByPath2(values, formula$1.fieldName, defaultValue);
904
+ setValueByPath2(data, formula$1.fieldName, defaultValue);
905
+ }
906
+ return [
907
+ createError(
908
+ formula$1,
909
+ "Formula returned undefined",
910
+ options.useDefaults ?? false
911
+ )
912
+ ];
913
+ }
914
+ setValueByPath2(values, formula$1.fieldName, result);
915
+ setValueByPath2(data, formula$1.fieldName, result);
916
+ return [];
917
+ } catch (error) {
918
+ if (options.useDefaults) {
919
+ const defaultValue = getDefaultValue(formula$1, options.defaults);
920
+ setValueByPath2(values, formula$1.fieldName, defaultValue);
921
+ setValueByPath2(data, formula$1.fieldName, defaultValue);
922
+ }
923
+ return [
924
+ createError(
925
+ formula$1,
926
+ error instanceof Error ? error.message : String(error),
927
+ options.useDefaults ?? false
928
+ )
929
+ ];
930
+ }
931
+ }
932
+ function evaluateArrayFormula(formula$1, data, values, options) {
933
+ const errors = [];
934
+ const arrayPath = formula$1.arrayPath;
935
+ const array = getValueByPath2(data, arrayPath);
936
+ if (!Array.isArray(array)) {
937
+ return errors;
938
+ }
939
+ for (let i = 0; i < array.length; i++) {
940
+ const item = array[i];
941
+ if (typeof item !== "object" || item === null) {
942
+ continue;
943
+ }
944
+ const itemData = item;
945
+ const fieldPath = `${arrayPath}[${i}].${formula$1.localFieldPath}`;
946
+ const context = {
947
+ rootData: data,
948
+ itemData,
949
+ currentPath: `${arrayPath}[${i}]`
950
+ };
951
+ try {
952
+ const result = formula.evaluateWithContext(formula$1.expression, context);
953
+ if (result === void 0) {
954
+ if (options.useDefaults) {
955
+ const defaultValue = getDefaultValue(formula$1, options.defaults);
956
+ setValueByPath2(values, fieldPath, defaultValue);
957
+ setValueByPath2(itemData, formula$1.localFieldPath, defaultValue);
958
+ }
959
+ errors.push({
960
+ field: fieldPath,
961
+ expression: formula$1.expression,
962
+ error: "Formula returned undefined",
963
+ defaultUsed: options.useDefaults ?? false
964
+ });
965
+ continue;
966
+ }
967
+ setValueByPath2(values, fieldPath, result);
968
+ setValueByPath2(itemData, formula$1.localFieldPath, result);
969
+ } catch (error) {
970
+ if (options.useDefaults) {
971
+ const defaultValue = getDefaultValue(formula$1, options.defaults);
972
+ setValueByPath2(values, fieldPath, defaultValue);
973
+ setValueByPath2(itemData, formula$1.localFieldPath, defaultValue);
974
+ }
975
+ errors.push({
976
+ field: fieldPath,
977
+ expression: formula$1.expression,
978
+ error: error instanceof Error ? error.message : String(error),
979
+ defaultUsed: options.useDefaults ?? false
980
+ });
981
+ }
982
+ }
983
+ return errors;
984
+ }
985
+ function getValueByPath2(obj, path) {
986
+ const segments = path.split(".");
987
+ let current = obj;
988
+ for (const segment of segments) {
989
+ if (current === null || current === void 0) {
990
+ return void 0;
991
+ }
992
+ current = current[segment];
993
+ }
994
+ return current;
995
+ }
996
+ function isSafeKey(key) {
997
+ return key !== "__proto__";
998
+ }
999
+ function setValueByPath2(obj, path, value) {
1000
+ const segments = path.split(".");
1001
+ let current = obj;
1002
+ for (let i = 0; i < segments.length - 1; i++) {
1003
+ const segment = segments[i];
1004
+ if (!isSafeKey(segment)) {
1005
+ return;
1006
+ }
1007
+ if (!(segment in current)) {
1008
+ current[segment] = {};
1009
+ }
1010
+ current = current[segment];
1011
+ }
1012
+ const lastSegment = segments.at(-1);
1013
+ if (!isSafeKey(lastSegment)) {
1014
+ return;
1015
+ }
1016
+ current[lastSegment] = value;
1017
+ }
1018
+ function getDefaultValue(formula, defaults) {
1019
+ if (defaults && formula.fieldName in defaults) {
1020
+ return defaults[formula.fieldName];
1021
+ }
1022
+ switch (formula.fieldType) {
1023
+ case "number":
1024
+ return 0;
1025
+ case "string":
1026
+ return "";
1027
+ case "boolean":
1028
+ return false;
1029
+ default:
1030
+ return null;
1031
+ }
1032
+ }
1033
+ function createError(formula, error, defaultUsed) {
1034
+ return {
1035
+ field: formula.fieldName,
1036
+ expression: formula.expression,
1037
+ error,
1038
+ defaultUsed
1039
+ };
1040
+ }
1041
+
574
1042
  // src/lib/getDBJsonPathByJsonSchemaStore.ts
575
1043
  var getDBJsonPathByJsonSchemaStore = (store) => {
576
1044
  let node = store;
@@ -824,6 +1292,10 @@ var SchemaTable = class {
824
1292
  }
825
1293
  };
826
1294
 
1295
+ Object.defineProperty(exports, "formulaSpec", {
1296
+ enumerable: true,
1297
+ get: function () { return spec.formulaSpec; }
1298
+ });
827
1299
  exports.SchemaTable = SchemaTable;
828
1300
  exports.VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE = VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE;
829
1301
  exports.applyAddPatch = applyAddPatch;
@@ -837,6 +1309,8 @@ exports.createJsonObjectSchemaStore = createJsonObjectSchemaStore;
837
1309
  exports.createJsonSchemaStore = createJsonSchemaStore;
838
1310
  exports.createPrimitiveStoreBySchema = createPrimitiveStoreBySchema;
839
1311
  exports.deepEqual = deepEqual;
1312
+ exports.evaluateFormulas = evaluateFormulas;
1313
+ exports.extractSchemaFormulas = extractSchemaFormulas;
840
1314
  exports.getDBJsonPathByJsonSchemaStore = getDBJsonPathByJsonSchemaStore;
841
1315
  exports.getForeignKeyPatchesFromSchema = getForeignKeyPatchesFromSchema;
842
1316
  exports.getForeignKeysFromSchema = getForeignKeysFromSchema;
@@ -850,12 +1324,15 @@ exports.getValueByPath = getValueByPath;
850
1324
  exports.hasPath = hasPath;
851
1325
  exports.parsePath = parsePath;
852
1326
  exports.pluginRefs = pluginRefs;
1327
+ exports.prepareFormulas = prepareFormulas;
853
1328
  exports.replaceForeignKeyValue = replaceForeignKeyValue;
854
1329
  exports.resolveRefs = resolveRefs;
855
1330
  exports.saveSharedFields = saveSharedFields;
856
1331
  exports.setValueByPath = setValueByPath;
857
1332
  exports.traverseStore = traverseStore;
858
1333
  exports.traverseValue = traverseValue;
1334
+ exports.validateFormulaAgainstSchema = validateFormulaAgainstSchema;
859
1335
  exports.validateJsonFieldName = validateJsonFieldName;
860
- //# sourceMappingURL=chunk-UJBE43WI.cjs.map
861
- //# sourceMappingURL=chunk-UJBE43WI.cjs.map
1336
+ exports.validateSchemaFormulas = validateSchemaFormulas;
1337
+ //# sourceMappingURL=chunk-3P7MTWNL.cjs.map
1338
+ //# sourceMappingURL=chunk-3P7MTWNL.cjs.map