@revisium/schema-toolkit 0.21.5 → 0.22.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.
@@ -2,6 +2,12 @@
2
2
 
3
3
  var chunkITYABUR5_cjs = require('./chunk-ITYABUR5.cjs');
4
4
  var chunkONW2OVNQ_cjs = require('./chunk-ONW2OVNQ.cjs');
5
+ var chunkC3HDJOTY_cjs = require('./chunk-C3HDJOTY.cjs');
6
+ var Ajv = require('ajv/dist/2020');
7
+
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var Ajv__default = /*#__PURE__*/_interopDefault(Ajv);
5
11
 
6
12
  // src/lib/createJsonSchemaStore.ts
7
13
  var createJsonSchemaStore = (schema, refs = {}) => {
@@ -731,13 +737,242 @@ var SchemaTable = class {
731
737
  this.store = nextStore;
732
738
  }
733
739
  };
740
+ var mapErrors = (errors) => {
741
+ if (!errors || errors.length === 0) {
742
+ return null;
743
+ }
744
+ return errors.map((err) => ({
745
+ instancePath: err.instancePath,
746
+ message: err.message,
747
+ keyword: err.keyword,
748
+ params: err.params
749
+ }));
750
+ };
751
+ var wrapValidateFn = (ajvFn) => {
752
+ const fn = ((data) => {
753
+ const result = ajvFn(data);
754
+ fn.errors = mapErrors(ajvFn.errors);
755
+ return result;
756
+ });
757
+ fn.errors = null;
758
+ return fn;
759
+ };
760
+ var RevisiumValidator = class {
761
+ validateMetaSchema;
762
+ validateJsonPatch;
763
+ validateMigrations;
764
+ validateHistoryPatches;
765
+ validateTableViews;
766
+ ajv;
767
+ cache = /* @__PURE__ */ new Map();
768
+ constructor() {
769
+ this.ajv = new Ajv__default.default();
770
+ this.ajv.addKeyword({
771
+ keyword: "foreignKey",
772
+ type: "string"
773
+ });
774
+ this.ajv.addKeyword({
775
+ keyword: "x-formula"
776
+ });
777
+ this.ajv.addFormat("regex", {
778
+ type: "string",
779
+ validate: (str) => {
780
+ try {
781
+ new RegExp(str);
782
+ return true;
783
+ } catch {
784
+ return false;
785
+ }
786
+ }
787
+ });
788
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowIdSchema);
789
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowCreatedIdSchema);
790
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowVersionIdSchema);
791
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowCreatedAtSchema);
792
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowPublishedAtSchema);
793
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowUpdatedAtSchema);
794
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowHashSchema);
795
+ this.ajv.compile(chunkITYABUR5_cjs.ajvRowSchemaHashSchema);
796
+ this.ajv.compile(chunkITYABUR5_cjs.ajvFileSchema);
797
+ this.validateMetaSchema = wrapValidateFn(this.ajv.compile(chunkC3HDJOTY_cjs.metaSchema));
798
+ this.validateJsonPatch = wrapValidateFn(this.ajv.compile(chunkC3HDJOTY_cjs.jsonPatchSchema));
799
+ this.validateMigrations = wrapValidateFn(
800
+ this.ajv.compile(chunkC3HDJOTY_cjs.tableMigrationsSchema)
801
+ );
802
+ this.validateHistoryPatches = wrapValidateFn(
803
+ this.ajv.compile(chunkC3HDJOTY_cjs.historyPatchesSchema)
804
+ );
805
+ this.validateTableViews = wrapValidateFn(
806
+ this.ajv.compile(chunkC3HDJOTY_cjs.tableViewsSchema)
807
+ );
808
+ }
809
+ compile(schema) {
810
+ const key = JSON.stringify(schema);
811
+ const cached = this.cache.get(key);
812
+ if (cached) {
813
+ return cached;
814
+ }
815
+ const fn = wrapValidateFn(
816
+ this.ajv.compile(schema)
817
+ );
818
+ this.cache.set(key, fn);
819
+ return fn;
820
+ }
821
+ };
822
+
823
+ // src/lib/validateRevisiumSchema.ts
824
+ var cachedValidator = null;
825
+ var getValidator = () => {
826
+ if (cachedValidator) {
827
+ return cachedValidator;
828
+ }
829
+ cachedValidator = new RevisiumValidator();
830
+ return cachedValidator;
831
+ };
832
+ var validateRevisiumSchema = (schema) => {
833
+ const validator = getValidator();
834
+ const valid = validator.validateMetaSchema(schema);
835
+ if (valid) {
836
+ return { valid: true };
837
+ }
838
+ const errors = (validator.validateMetaSchema.errors ?? []).map((err) => {
839
+ const path = err.instancePath || "/";
840
+ return `${path}: ${err.message ?? "unknown error"}`;
841
+ });
842
+ return { valid: false, errors };
843
+ };
844
+
845
+ // src/lib/calculateSchemaWeight.ts
846
+ var calculateSchemaWeight = (schema) => {
847
+ const result = {
848
+ totalFields: 0,
849
+ maxDepth: 0,
850
+ fieldNames: 0,
851
+ totalArrays: 0,
852
+ maxArrayDepth: 0
853
+ };
854
+ walkSchema(schema, 0, 0, result);
855
+ return result;
856
+ };
857
+ var walkSchema = (schema, depth, arrayDepth, result) => {
858
+ if (result.maxDepth < depth) {
859
+ result.maxDepth = depth;
860
+ }
861
+ if ("$ref" in schema) {
862
+ return;
863
+ }
864
+ if (schema.type === "object" && schema.properties) {
865
+ for (const [fieldName, fieldSchema] of Object.entries(schema.properties)) {
866
+ result.totalFields++;
867
+ result.fieldNames += fieldName.length;
868
+ walkSchema(fieldSchema, depth + 1, arrayDepth, result);
869
+ }
870
+ }
871
+ if (schema.type === "array" && schema.items) {
872
+ const nextArrayDepth = arrayDepth + 1;
873
+ result.totalArrays++;
874
+ if (nextArrayDepth > result.maxArrayDepth) {
875
+ result.maxArrayDepth = nextArrayDepth;
876
+ }
877
+ walkSchema(schema.items, depth + 1, nextArrayDepth, result);
878
+ }
879
+ };
880
+
881
+ // src/lib/calculateDataWeight.ts
882
+ var calculateDataWeight = (data) => {
883
+ const result = {
884
+ totalBytes: 0,
885
+ totalNodes: 0,
886
+ maxDepth: 0,
887
+ maxArrayLength: 0,
888
+ maxStringLength: 0,
889
+ totalStringsLength: 0
890
+ };
891
+ result.totalBytes = JSON.stringify(data).length;
892
+ walkValue(data, 0, result);
893
+ return result;
894
+ };
895
+ var walkValue = (value, depth, result) => {
896
+ result.totalNodes++;
897
+ if (result.maxDepth < depth) {
898
+ result.maxDepth = depth;
899
+ }
900
+ if (value === null || value === void 0) {
901
+ return;
902
+ }
903
+ if (typeof value === "string") {
904
+ if (value.length > result.maxStringLength) {
905
+ result.maxStringLength = value.length;
906
+ }
907
+ result.totalStringsLength += value.length;
908
+ return;
909
+ }
910
+ if (typeof value === "number" || typeof value === "boolean") {
911
+ return;
912
+ }
913
+ if (Array.isArray(value)) {
914
+ if (value.length > result.maxArrayLength) {
915
+ result.maxArrayLength = value.length;
916
+ }
917
+ for (const item of value) {
918
+ walkValue(item, depth + 1, result);
919
+ }
920
+ return;
921
+ }
922
+ if (typeof value === "object") {
923
+ for (const val of Object.values(value)) {
924
+ walkValue(val, depth + 1, result);
925
+ }
926
+ }
927
+ };
928
+ var calculateDataWeightFromStore = (store) => {
929
+ const result = {
930
+ totalBytes: 0,
931
+ totalNodes: 0,
932
+ maxDepth: 0,
933
+ maxArrayLength: 0,
934
+ maxStringLength: 0,
935
+ totalStringsLength: 0
936
+ };
937
+ result.totalBytes = JSON.stringify(store.getPlainValue()).length;
938
+ walkStore(store, 0, result);
939
+ return result;
940
+ };
941
+ var walkStore = (store, depth, result) => {
942
+ result.totalNodes++;
943
+ if (result.maxDepth < depth) {
944
+ result.maxDepth = depth;
945
+ }
946
+ if (store.type === "string") {
947
+ const val = store.getPlainValue();
948
+ if (val.length > result.maxStringLength) {
949
+ result.maxStringLength = val.length;
950
+ }
951
+ result.totalStringsLength += val.length;
952
+ } else if (store.type === "object") {
953
+ for (const child of Object.values(store.value)) {
954
+ walkStore(child, depth + 1, result);
955
+ }
956
+ } else if (store.type === "array") {
957
+ if (store.value.length > result.maxArrayLength) {
958
+ result.maxArrayLength = store.value.length;
959
+ }
960
+ for (const child of store.value) {
961
+ walkStore(child, depth + 1, result);
962
+ }
963
+ }
964
+ };
734
965
 
966
+ exports.RevisiumValidator = RevisiumValidator;
735
967
  exports.SchemaTable = SchemaTable;
736
968
  exports.VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE = VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE;
737
969
  exports.applyAddPatch = applyAddPatch;
738
970
  exports.applyMovePatch = applyMovePatch;
739
971
  exports.applyRemovePatch = applyRemovePatch;
740
972
  exports.applyReplacePatch = applyReplacePatch;
973
+ exports.calculateDataWeight = calculateDataWeight;
974
+ exports.calculateDataWeightFromStore = calculateDataWeightFromStore;
975
+ exports.calculateSchemaWeight = calculateSchemaWeight;
741
976
  exports.convertJsonPathToSchemaPath = convertJsonPathToSchemaPath;
742
977
  exports.convertSchemaPathToJsonPath = convertSchemaPathToJsonPath;
743
978
  exports.createJsonObjectSchemaStore = createJsonObjectSchemaStore;
@@ -764,3 +999,4 @@ exports.setValueByPath = setValueByPath;
764
999
  exports.traverseStore = traverseStore;
765
1000
  exports.traverseValue = traverseValue;
766
1001
  exports.validateJsonFieldName = validateJsonFieldName;
1002
+ exports.validateRevisiumSchema = validateRevisiumSchema;
@@ -1,5 +1,7 @@
1
- import { fileSchema, rowSchemaHashSchema, rowHashSchema, rowUpdatedAtSchema, rowPublishedAtSchema, rowCreatedAtSchema, rowCreatedIdSchema, rowVersionIdSchema, rowIdSchema } from './chunk-4U2RZHMX.js';
1
+ import { fileSchema, rowSchemaHashSchema, rowHashSchema, rowUpdatedAtSchema, rowPublishedAtSchema, rowCreatedAtSchema, rowCreatedIdSchema, rowVersionIdSchema, rowIdSchema, ajvRowIdSchema, ajvRowCreatedIdSchema, ajvRowVersionIdSchema, ajvRowCreatedAtSchema, ajvRowPublishedAtSchema, ajvRowUpdatedAtSchema, ajvRowHashSchema, ajvRowSchemaHashSchema, ajvFileSchema } from './chunk-4U2RZHMX.js';
2
2
  import { JsonArrayStore, JsonObjectStore, JsonStringStore, JsonNumberStore, JsonBooleanStore, JsonObjectValueStore, JsonArrayValueStore, createJsonValueStore, getTransformation } from './chunk-FTBRJODD.js';
3
+ import { metaSchema, jsonPatchSchema, tableMigrationsSchema, historyPatchesSchema, tableViewsSchema } from './chunk-6IMYMTFF.js';
4
+ import Ajv from 'ajv/dist/2020';
3
5
 
4
6
  // src/lib/createJsonSchemaStore.ts
5
7
  var createJsonSchemaStore = (schema, refs = {}) => {
@@ -729,5 +731,230 @@ var SchemaTable = class {
729
731
  this.store = nextStore;
730
732
  }
731
733
  };
734
+ var mapErrors = (errors) => {
735
+ if (!errors || errors.length === 0) {
736
+ return null;
737
+ }
738
+ return errors.map((err) => ({
739
+ instancePath: err.instancePath,
740
+ message: err.message,
741
+ keyword: err.keyword,
742
+ params: err.params
743
+ }));
744
+ };
745
+ var wrapValidateFn = (ajvFn) => {
746
+ const fn = ((data) => {
747
+ const result = ajvFn(data);
748
+ fn.errors = mapErrors(ajvFn.errors);
749
+ return result;
750
+ });
751
+ fn.errors = null;
752
+ return fn;
753
+ };
754
+ var RevisiumValidator = class {
755
+ validateMetaSchema;
756
+ validateJsonPatch;
757
+ validateMigrations;
758
+ validateHistoryPatches;
759
+ validateTableViews;
760
+ ajv;
761
+ cache = /* @__PURE__ */ new Map();
762
+ constructor() {
763
+ this.ajv = new Ajv();
764
+ this.ajv.addKeyword({
765
+ keyword: "foreignKey",
766
+ type: "string"
767
+ });
768
+ this.ajv.addKeyword({
769
+ keyword: "x-formula"
770
+ });
771
+ this.ajv.addFormat("regex", {
772
+ type: "string",
773
+ validate: (str) => {
774
+ try {
775
+ new RegExp(str);
776
+ return true;
777
+ } catch {
778
+ return false;
779
+ }
780
+ }
781
+ });
782
+ this.ajv.compile(ajvRowIdSchema);
783
+ this.ajv.compile(ajvRowCreatedIdSchema);
784
+ this.ajv.compile(ajvRowVersionIdSchema);
785
+ this.ajv.compile(ajvRowCreatedAtSchema);
786
+ this.ajv.compile(ajvRowPublishedAtSchema);
787
+ this.ajv.compile(ajvRowUpdatedAtSchema);
788
+ this.ajv.compile(ajvRowHashSchema);
789
+ this.ajv.compile(ajvRowSchemaHashSchema);
790
+ this.ajv.compile(ajvFileSchema);
791
+ this.validateMetaSchema = wrapValidateFn(this.ajv.compile(metaSchema));
792
+ this.validateJsonPatch = wrapValidateFn(this.ajv.compile(jsonPatchSchema));
793
+ this.validateMigrations = wrapValidateFn(
794
+ this.ajv.compile(tableMigrationsSchema)
795
+ );
796
+ this.validateHistoryPatches = wrapValidateFn(
797
+ this.ajv.compile(historyPatchesSchema)
798
+ );
799
+ this.validateTableViews = wrapValidateFn(
800
+ this.ajv.compile(tableViewsSchema)
801
+ );
802
+ }
803
+ compile(schema) {
804
+ const key = JSON.stringify(schema);
805
+ const cached = this.cache.get(key);
806
+ if (cached) {
807
+ return cached;
808
+ }
809
+ const fn = wrapValidateFn(
810
+ this.ajv.compile(schema)
811
+ );
812
+ this.cache.set(key, fn);
813
+ return fn;
814
+ }
815
+ };
816
+
817
+ // src/lib/validateRevisiumSchema.ts
818
+ var cachedValidator = null;
819
+ var getValidator = () => {
820
+ if (cachedValidator) {
821
+ return cachedValidator;
822
+ }
823
+ cachedValidator = new RevisiumValidator();
824
+ return cachedValidator;
825
+ };
826
+ var validateRevisiumSchema = (schema) => {
827
+ const validator = getValidator();
828
+ const valid = validator.validateMetaSchema(schema);
829
+ if (valid) {
830
+ return { valid: true };
831
+ }
832
+ const errors = (validator.validateMetaSchema.errors ?? []).map((err) => {
833
+ const path = err.instancePath || "/";
834
+ return `${path}: ${err.message ?? "unknown error"}`;
835
+ });
836
+ return { valid: false, errors };
837
+ };
838
+
839
+ // src/lib/calculateSchemaWeight.ts
840
+ var calculateSchemaWeight = (schema) => {
841
+ const result = {
842
+ totalFields: 0,
843
+ maxDepth: 0,
844
+ fieldNames: 0,
845
+ totalArrays: 0,
846
+ maxArrayDepth: 0
847
+ };
848
+ walkSchema(schema, 0, 0, result);
849
+ return result;
850
+ };
851
+ var walkSchema = (schema, depth, arrayDepth, result) => {
852
+ if (result.maxDepth < depth) {
853
+ result.maxDepth = depth;
854
+ }
855
+ if ("$ref" in schema) {
856
+ return;
857
+ }
858
+ if (schema.type === "object" && schema.properties) {
859
+ for (const [fieldName, fieldSchema] of Object.entries(schema.properties)) {
860
+ result.totalFields++;
861
+ result.fieldNames += fieldName.length;
862
+ walkSchema(fieldSchema, depth + 1, arrayDepth, result);
863
+ }
864
+ }
865
+ if (schema.type === "array" && schema.items) {
866
+ const nextArrayDepth = arrayDepth + 1;
867
+ result.totalArrays++;
868
+ if (nextArrayDepth > result.maxArrayDepth) {
869
+ result.maxArrayDepth = nextArrayDepth;
870
+ }
871
+ walkSchema(schema.items, depth + 1, nextArrayDepth, result);
872
+ }
873
+ };
874
+
875
+ // src/lib/calculateDataWeight.ts
876
+ var calculateDataWeight = (data) => {
877
+ const result = {
878
+ totalBytes: 0,
879
+ totalNodes: 0,
880
+ maxDepth: 0,
881
+ maxArrayLength: 0,
882
+ maxStringLength: 0,
883
+ totalStringsLength: 0
884
+ };
885
+ result.totalBytes = JSON.stringify(data).length;
886
+ walkValue(data, 0, result);
887
+ return result;
888
+ };
889
+ var walkValue = (value, depth, result) => {
890
+ result.totalNodes++;
891
+ if (result.maxDepth < depth) {
892
+ result.maxDepth = depth;
893
+ }
894
+ if (value === null || value === void 0) {
895
+ return;
896
+ }
897
+ if (typeof value === "string") {
898
+ if (value.length > result.maxStringLength) {
899
+ result.maxStringLength = value.length;
900
+ }
901
+ result.totalStringsLength += value.length;
902
+ return;
903
+ }
904
+ if (typeof value === "number" || typeof value === "boolean") {
905
+ return;
906
+ }
907
+ if (Array.isArray(value)) {
908
+ if (value.length > result.maxArrayLength) {
909
+ result.maxArrayLength = value.length;
910
+ }
911
+ for (const item of value) {
912
+ walkValue(item, depth + 1, result);
913
+ }
914
+ return;
915
+ }
916
+ if (typeof value === "object") {
917
+ for (const val of Object.values(value)) {
918
+ walkValue(val, depth + 1, result);
919
+ }
920
+ }
921
+ };
922
+ var calculateDataWeightFromStore = (store) => {
923
+ const result = {
924
+ totalBytes: 0,
925
+ totalNodes: 0,
926
+ maxDepth: 0,
927
+ maxArrayLength: 0,
928
+ maxStringLength: 0,
929
+ totalStringsLength: 0
930
+ };
931
+ result.totalBytes = JSON.stringify(store.getPlainValue()).length;
932
+ walkStore(store, 0, result);
933
+ return result;
934
+ };
935
+ var walkStore = (store, depth, result) => {
936
+ result.totalNodes++;
937
+ if (result.maxDepth < depth) {
938
+ result.maxDepth = depth;
939
+ }
940
+ if (store.type === "string") {
941
+ const val = store.getPlainValue();
942
+ if (val.length > result.maxStringLength) {
943
+ result.maxStringLength = val.length;
944
+ }
945
+ result.totalStringsLength += val.length;
946
+ } else if (store.type === "object") {
947
+ for (const child of Object.values(store.value)) {
948
+ walkStore(child, depth + 1, result);
949
+ }
950
+ } else if (store.type === "array") {
951
+ if (store.value.length > result.maxArrayLength) {
952
+ result.maxArrayLength = store.value.length;
953
+ }
954
+ for (const child of store.value) {
955
+ walkStore(child, depth + 1, result);
956
+ }
957
+ }
958
+ };
732
959
 
733
- export { SchemaTable, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, convertJsonPathToSchemaPath, convertSchemaPathToJsonPath, createJsonObjectSchemaStore, createJsonSchemaStore, createPrimitiveStoreBySchema, deepEqual, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getParentForPath, getPathByStore, getValueByPath, hasPath, parsePath, pluginRefs, replaceForeignKeyValue, resolveRefs, saveSharedFields, setValueByPath, traverseStore, traverseValue, validateJsonFieldName };
960
+ export { RevisiumValidator, SchemaTable, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, calculateDataWeight, calculateDataWeightFromStore, calculateSchemaWeight, convertJsonPathToSchemaPath, convertSchemaPathToJsonPath, createJsonObjectSchemaStore, createJsonSchemaStore, createPrimitiveStoreBySchema, deepEqual, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getParentForPath, getPathByStore, getValueByPath, hasPath, parsePath, pluginRefs, replaceForeignKeyValue, resolveRefs, saveSharedFields, setValueByPath, traverseStore, traverseValue, validateJsonFieldName, validateRevisiumSchema };