@revisium/schema-toolkit 0.21.4 → 0.22.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.
Files changed (34) hide show
  1. package/README.md +63 -2
  2. package/dist/{chunk-MXEQ5CBK.js → chunk-2GZ3M3RV.js} +3 -0
  3. package/dist/chunk-5G3AVY57.js +1 -0
  4. package/dist/chunk-5M74TGFQ.cjs +2 -0
  5. package/dist/{chunk-3MXYXTNF.js → chunk-B3W5BASK.js} +22 -3
  6. package/dist/{chunk-JLXX3SGF.js → chunk-GAP5KNWN.js} +229 -2
  7. package/dist/{chunk-XKWWCXGB.js → chunk-HOMXBUQB.js} +143 -20
  8. package/dist/{chunk-URMXJ6FH.cjs → chunk-JHNATNUI.cjs} +90 -71
  9. package/dist/{chunk-T46TZYJL.cjs → chunk-PJ5OFCLO.cjs} +3 -0
  10. package/dist/{chunk-QKA7DJDN.cjs → chunk-WY3CL7KH.cjs} +143 -19
  11. package/dist/{chunk-7JWCZZPJ.cjs → chunk-YZEJKK4O.cjs} +236 -0
  12. package/dist/core/index.cjs +60 -60
  13. package/dist/core/index.js +1 -1
  14. package/dist/index.cjs +182 -157
  15. package/dist/index.d.cts +3 -3
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.js +5 -4
  18. package/dist/lib/index.cjs +54 -33
  19. package/dist/lib/index.d.cts +49 -1
  20. package/dist/lib/index.d.ts +49 -1
  21. package/dist/lib/index.js +2 -1
  22. package/dist/model/index.cjs +50 -50
  23. package/dist/model/index.d.cts +11 -3
  24. package/dist/model/index.d.ts +11 -3
  25. package/dist/model/index.js +2 -2
  26. package/dist/{typed-DjSH_5Vh.d.cts → typed-C0YM-ar9.d.cts} +6 -0
  27. package/dist/{typed-KUia-LYN.d.ts → typed-CkirI6bH.d.ts} +6 -0
  28. package/dist/types/index.d.cts +1 -1
  29. package/dist/types/index.d.ts +1 -1
  30. package/dist/validation-schemas/index.cjs +23 -18
  31. package/dist/validation-schemas/index.d.cts +3 -1
  32. package/dist/validation-schemas/index.d.ts +3 -1
  33. package/dist/validation-schemas/index.js +2 -1
  34. package/package.json +2 -2
@@ -319,25 +319,6 @@ var jsonPatchSchema = {
319
319
  type: "array"
320
320
  };
321
321
 
322
- // src/validation-schemas/history-patches-schema.ts
323
- var historyPatchesSchema = {
324
- $id: "history-patches-schema.json",
325
- type: "array",
326
- minItems: 1,
327
- items: {
328
- type: "object",
329
- properties: {
330
- patches: {
331
- $ref: "json-patch-schema.json"
332
- },
333
- hash: {
334
- type: "string"
335
- }
336
- },
337
- required: ["patches", "hash"]
338
- }
339
- };
340
-
341
322
  // src/validation-schemas/table-migrations-schema.ts
342
323
  var tableMigrationsSchema = {
343
324
  $id: "table-migrations-schema.json",
@@ -397,6 +378,148 @@ var tableMigrationsSchema = {
397
378
  title: "JSON Schema for a Single Migration"
398
379
  };
399
380
 
381
+ // src/validation-schemas/history-patches-schema.ts
382
+ var historyPatchesSchema = {
383
+ $id: "history-patches-schema.json",
384
+ type: "array",
385
+ minItems: 1,
386
+ items: {
387
+ type: "object",
388
+ properties: {
389
+ patches: {
390
+ $ref: "json-patch-schema.json"
391
+ },
392
+ hash: {
393
+ type: "string"
394
+ }
395
+ },
396
+ required: ["patches", "hash"]
397
+ }
398
+ };
399
+
400
+ // src/validation-schemas/table-views-schema.ts
401
+ var tableViewsSchema = {
402
+ $id: "table-views-schema.json",
403
+ type: "object",
404
+ additionalProperties: false,
405
+ required: ["version", "views"],
406
+ properties: {
407
+ version: {
408
+ type: "integer",
409
+ minimum: 1,
410
+ default: 1
411
+ },
412
+ defaultViewId: {
413
+ type: "string",
414
+ default: "default"
415
+ },
416
+ views: {
417
+ type: "array",
418
+ items: { $ref: "#/$defs/View" },
419
+ default: []
420
+ }
421
+ },
422
+ $defs: {
423
+ View: {
424
+ type: "object",
425
+ additionalProperties: false,
426
+ required: ["id", "name"],
427
+ properties: {
428
+ id: { type: "string", minLength: 1 },
429
+ name: { type: "string", minLength: 1, maxLength: 100 },
430
+ description: { type: "string", maxLength: 500, default: "" },
431
+ columns: {
432
+ oneOf: [
433
+ { type: "null" },
434
+ { type: "array", items: { $ref: "#/$defs/Column" } }
435
+ ],
436
+ default: null
437
+ },
438
+ filters: { $ref: "#/$defs/FilterGroup" },
439
+ sorts: {
440
+ type: "array",
441
+ items: { $ref: "#/$defs/Sort" },
442
+ default: []
443
+ },
444
+ search: {
445
+ type: "string",
446
+ default: ""
447
+ }
448
+ }
449
+ },
450
+ Column: {
451
+ type: "object",
452
+ additionalProperties: false,
453
+ required: ["field"],
454
+ properties: {
455
+ field: { type: "string", minLength: 1 },
456
+ width: { type: "number", minimum: 40 }
457
+ }
458
+ },
459
+ FilterGroup: {
460
+ type: "object",
461
+ additionalProperties: false,
462
+ properties: {
463
+ logic: {
464
+ type: "string",
465
+ enum: ["and", "or"],
466
+ default: "and"
467
+ },
468
+ conditions: {
469
+ type: "array",
470
+ items: { $ref: "#/$defs/FilterCondition" },
471
+ default: []
472
+ },
473
+ groups: {
474
+ type: "array",
475
+ items: { $ref: "#/$defs/FilterGroup" },
476
+ default: []
477
+ }
478
+ }
479
+ },
480
+ FilterCondition: {
481
+ type: "object",
482
+ additionalProperties: false,
483
+ required: ["field", "operator"],
484
+ properties: {
485
+ field: { type: "string", minLength: 1 },
486
+ operator: {
487
+ type: "string",
488
+ enum: [
489
+ "equals",
490
+ "not_equals",
491
+ "contains",
492
+ "not_contains",
493
+ "starts_with",
494
+ "ends_with",
495
+ "is_empty",
496
+ "is_not_empty",
497
+ "gt",
498
+ "gte",
499
+ "lt",
500
+ "lte",
501
+ "is_true",
502
+ "is_false"
503
+ ]
504
+ },
505
+ value: {}
506
+ }
507
+ },
508
+ Sort: {
509
+ type: "object",
510
+ additionalProperties: false,
511
+ required: ["field", "direction"],
512
+ properties: {
513
+ field: { type: "string", minLength: 1 },
514
+ direction: {
515
+ type: "string",
516
+ enum: ["asc", "desc"]
517
+ }
518
+ }
519
+ }
520
+ }
521
+ };
522
+
400
523
  exports.arrayMetaSchema = arrayMetaSchema;
401
524
  exports.baseStringFields = baseStringFields;
402
525
  exports.booleanMetaSchema = booleanMetaSchema;
@@ -412,5 +535,6 @@ exports.refMetaSchema = refMetaSchema;
412
535
  exports.sharedFields = sharedFields;
413
536
  exports.stringMetaSchema = stringMetaSchema;
414
537
  exports.tableMigrationsSchema = tableMigrationsSchema;
538
+ exports.tableViewsSchema = tableViewsSchema;
415
539
  exports.xFormulaRequiresReadOnly = xFormulaRequiresReadOnly;
416
540
  exports.xFormulaSchema = xFormulaSchema;
@@ -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 chunkWY3CL7KH_cjs = require('./chunk-WY3CL7KH.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(chunkWY3CL7KH_cjs.metaSchema));
798
+ this.validateJsonPatch = wrapValidateFn(this.ajv.compile(chunkWY3CL7KH_cjs.jsonPatchSchema));
799
+ this.validateMigrations = wrapValidateFn(
800
+ this.ajv.compile(chunkWY3CL7KH_cjs.tableMigrationsSchema)
801
+ );
802
+ this.validateHistoryPatches = wrapValidateFn(
803
+ this.ajv.compile(chunkWY3CL7KH_cjs.historyPatchesSchema)
804
+ );
805
+ this.validateTableViews = wrapValidateFn(
806
+ this.ajv.compile(chunkWY3CL7KH_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;