@jarenjs/validate 0.8.2 → 0.8.4

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
@@ -367,6 +367,13 @@ function equalsDeep(target, source) {
367
367
  }
368
368
  return true;
369
369
  }
370
+ function mapMerge(map, ...iterables) {
371
+ for (const iterable of iterables) {
372
+ for (const item of iterable) {
373
+ map.set(...item);
374
+ }
375
+ }
376
+ }
370
377
 
371
378
  // src/enum.js
372
379
  function compileConst(schemaObj, jsonSchema) {
@@ -1360,15 +1367,15 @@ function compileSchemaObject(schemaObj, jsonSchema) {
1360
1367
  };
1361
1368
  }
1362
1369
 
1363
- // ../strings/src/punycode.js
1370
+ // ../core/src/text/punycode.js
1364
1371
  var base = 36;
1365
1372
  var tMin = 1;
1366
1373
  var baseMinusTMin = base - tMin;
1367
1374
  var stringFromCharCode = String.fromCharCode;
1368
1375
 
1369
- // ../strings/src/index.js
1376
+ // ../core/src/text/index.js
1370
1377
  var CONST_REGEXP_HTML_IDENTIFIER = /^[A-Za-z]+[\w\-\:\.]{0,30}$/;
1371
- function isStringHtmlIdentifier(str) {
1378
+ function isValidHtmlIdentifier(str) {
1372
1379
  return (
1373
1380
  /* str != null && */
1374
1381
  CONST_REGEXP_HTML_IDENTIFIER.test(str)
@@ -1408,7 +1415,7 @@ function createJsonPointer(refUri, baseUri, opts = new JsonPointerOptions()) {
1408
1415
  const [uri, fragment] = url.href.split("#");
1409
1416
  const [leftUri, search] = uri.split("?");
1410
1417
  if (!isStringWhiteSpace(fragment)) {
1411
- if (opts.anchorsAllowed == true && isStringHtmlIdentifier(fragment)) {
1418
+ if (opts.anchorsAllowed == true && isValidHtmlIdentifier(fragment)) {
1412
1419
  return opts.anchorsGlobal == true ? new JsonPointer(`#${fragment}`, search, `${leftUri}#`, fragment) : new JsonPointer(`${leftUri}#${fragment}`, search, `${leftUri}#`, fragment);
1413
1420
  } else if (fragment.startsWith("/")) {
1414
1421
  return new JsonPointer(`${leftUri}#${fragment}`, search, `${leftUri}#`, fragment);
@@ -1445,8 +1452,9 @@ var TRAVERSE_SCHEMA_MAPS = [
1445
1452
  ];
1446
1453
  function storeSchemaIdsInMap(schemas, baseUri, schema, opts = new JsonPointerOptions()) {
1447
1454
  if (!isObjectClass(schema)) {
1448
- schemas.set(baseUri, schema);
1449
- return null;
1455
+ const { id } = createJsonPointer(baseUri, void 0, opts);
1456
+ schemas.set(id, schema);
1457
+ return id;
1450
1458
  }
1451
1459
  const { id: rootUri } = createJsonPointer(schema.$id, baseUri, opts);
1452
1460
  if (!isStringType(schema.$id) || isStringWhiteSpace(schema.$id)) {
@@ -1558,7 +1566,7 @@ function restoreSchemaRefsInMap(schemas, opts = new JsonPointerOptions()) {
1558
1566
  }
1559
1567
  }
1560
1568
  var TraverseOptions = class extends JsonPointerOptions {
1561
- constructor(origin = "https://github.com/jklarenbeek/jaren", mergeSchemas = true, anchorsGlobal, anchorsAllowed, skipErrors) {
1569
+ constructor(origin = "https://github.com/jklarenbeek/jaren", mergeSchemas = true, anchorsGlobal = true, anchorsAllowed = true, skipErrors = true) {
1562
1570
  super(anchorsGlobal, anchorsAllowed, skipErrors);
1563
1571
  this.origin = origin, this.mergeSchemas = mergeSchemas;
1564
1572
  }
@@ -1590,6 +1598,7 @@ function resolveRefSchemaDeep(schemas, baseUri, refschema, opts = new TraverseOp
1590
1598
  }
1591
1599
 
1592
1600
  // src/index.js
1601
+ var DEFAULT_SCHEMA_DRAFT = "http://json-schema.org/draft-06/schema#";
1593
1602
  var isBrowser = typeof window !== "undefined";
1594
1603
  var performance = (() => isBrowser ? window.performance : {
1595
1604
  now: function performanceNow(start) {
@@ -1627,12 +1636,13 @@ var ValidationRoot = class _ValidationRoot {
1627
1636
  objects.set(path, obj);
1628
1637
  return obj;
1629
1638
  }
1630
- constructor(schemas, schema, origin, formats, opts = new ValidationOptions(), traverse = new TraverseOptions()) {
1631
- this._options = opts;
1632
- this._traverse = traverse;
1633
- this._schemas = schemas;
1639
+ constructor(origin, schemas, formats, opts = new ValidationOptions(), traverse = new TraverseOptions()) {
1640
+ const schema = schemas.get(origin);
1634
1641
  this._rootOrigin = origin;
1642
+ this._schemas = schemas;
1635
1643
  this._formats = formats;
1644
+ this._options = opts;
1645
+ this._traverse = traverse;
1636
1646
  this._objects = /* @__PURE__ */ new Map();
1637
1647
  this._errors = [];
1638
1648
  this._firstSchema = _ValidationRoot._createObject(this, origin, schema);
@@ -1643,6 +1653,9 @@ var ValidationRoot = class _ValidationRoot {
1643
1653
  get formats() {
1644
1654
  return this._formats;
1645
1655
  }
1656
+ get errors() {
1657
+ return this._errors;
1658
+ }
1646
1659
  createObject(path, schema) {
1647
1660
  return _ValidationRoot._createObject(this, path, schema);
1648
1661
  }
@@ -1670,7 +1683,7 @@ var ValidationRoot = class _ValidationRoot {
1670
1683
  return false;
1671
1684
  }
1672
1685
  validate(data) {
1673
- this._errors.length = 0;
1686
+ this._errors = [];
1674
1687
  return this._firstSchema.validate(data, data);
1675
1688
  }
1676
1689
  };
@@ -1782,9 +1795,8 @@ function compileSchemaValidator(schema, opts = new ValidatorOptions()) {
1782
1795
  ));
1783
1796
  restoreSchemaRefsInMap(schemas, opts.traverse);
1784
1797
  const root = new ValidationRoot(
1785
- schemas,
1786
- schema,
1787
1798
  origin,
1799
+ schemas,
1788
1800
  opts.formats,
1789
1801
  opts.validation,
1790
1802
  opts.traverse
@@ -1793,7 +1805,191 @@ function compileSchemaValidator(schema, opts = new ValidatorOptions()) {
1793
1805
  validate: (data) => root.validate(data)
1794
1806
  };
1795
1807
  }
1808
+ var JarenValidator = class _JarenValidator {
1809
+ #formats = {};
1810
+ #schemas = /* @__PURE__ */ new Map();
1811
+ #metaSchemas = /* @__PURE__ */ new Map();
1812
+ #options = new ValidatorOptions();
1813
+ /**
1814
+ * Create a new JarenValidator instance
1815
+ * @param {ValidatorOptions} [options] - Validator options
1816
+ */
1817
+ constructor(options = new ValidatorOptions()) {
1818
+ this.#formats = options.formats || {};
1819
+ this.#schemas = /* @__PURE__ */ new Map();
1820
+ this.#metaSchemas = /* @__PURE__ */ new Map();
1821
+ this.#options = options;
1822
+ }
1823
+ /**
1824
+ * Add format to validate strings or numbers.
1825
+ * @param {string} name
1826
+ * @param {Function} formatCompiler
1827
+ * @returns {JarenValidator}
1828
+ */
1829
+ addFormat(name, formatCompiler) {
1830
+ registerFormatCompiler(
1831
+ this.#formats,
1832
+ name,
1833
+ formatCompiler
1834
+ );
1835
+ return this;
1836
+ }
1837
+ addFormats(formatCompilers) {
1838
+ registerFormatCompilers(
1839
+ this.#formats,
1840
+ formatCompilers
1841
+ );
1842
+ return this;
1843
+ }
1844
+ /**
1845
+ *
1846
+ * @param {boolean | object} schema
1847
+ * @param {object[] | undefined} schemas
1848
+ * @param {TraverseOptions} opts
1849
+ * @returns {{origin:string, map: Map}}
1850
+ */
1851
+ static #traverseSchema(schema, schemas = void 0, fromMap = void 0, opts = new TraverseOptions()) {
1852
+ const schemaMap = new Map(fromMap);
1853
+ const origin = storeSchemaIdsInMap(
1854
+ schemaMap,
1855
+ opts.origin,
1856
+ schema,
1857
+ opts
1858
+ );
1859
+ if (Array.isArray(schemas) && schemas.length > 0) {
1860
+ schemas.forEach((ref) => storeSchemaIdsInMap(
1861
+ schemaMap,
1862
+ origin,
1863
+ ref,
1864
+ opts
1865
+ ));
1866
+ }
1867
+ restoreSchemaRefsInMap(schemaMap, opts);
1868
+ return { origin, map: schemaMap };
1869
+ }
1870
+ /**
1871
+ * Add schema(s) to validator instance.
1872
+ * This method does not compile schemas (but it still validates them).
1873
+ * Because of that dependencies can be added in any order and
1874
+ * circular dependencies are supported.
1875
+ * It also prevents unnecessary compilation of schemas that are
1876
+ * containers for other schemas but not used as a whole.
1877
+ * @param {boolean | object | object[]} schema
1878
+ * @param {string | undefined} key
1879
+ * @returns {JarenValidator}
1880
+ */
1881
+ addSchema(schema, key = void 0) {
1882
+ if (Array.isArray(schema)) {
1883
+ schema.forEach((s, index) => this.addSchema(s, key ? `${key}[${index}]` : void 0));
1884
+ } else if (typeof schema === "object") {
1885
+ const schemaKey = key || schema.$id;
1886
+ if (schemaKey) {
1887
+ this.#schemas.set(schemaKey, schema);
1888
+ }
1889
+ }
1890
+ return this;
1891
+ }
1892
+ /**
1893
+ *
1894
+ * @param {JarenValidator} self
1895
+ * @param {string} origin
1896
+ * @param {Map} schemas
1897
+ * @returns {(data) => boolean}
1898
+ */
1899
+ static #compileSchema(self, origin, schemas) {
1900
+ const root = new ValidationRoot(
1901
+ origin,
1902
+ schemas,
1903
+ self.#formats,
1904
+ self.#options.validation,
1905
+ self.#options.traverse
1906
+ );
1907
+ function jarenValidateSchema(data) {
1908
+ return root.validate(data);
1909
+ }
1910
+ Object.defineProperty(jarenValidateSchema, "errors", {
1911
+ get: function() {
1912
+ return root._errors;
1913
+ }
1914
+ });
1915
+ return jarenValidateSchema;
1916
+ }
1917
+ static normalizeUriKey(key) {
1918
+ return key;
1919
+ }
1920
+ /**
1921
+ * Adds meta schema(s) that can be used to validate other schemas.
1922
+ * @param {boolean | object | object[]} schema
1923
+ * @param {string | undefined} key
1924
+ * @returns {JarenValidator}
1925
+ */
1926
+ addMetaSchema(schema, key = void 0) {
1927
+ key = _JarenValidator.normalizeUriKey(key);
1928
+ if (Array.isArray(schema)) {
1929
+ const first = schema.shift();
1930
+ const { origin, map } = _JarenValidator.#traverseSchema(first, schema, void 0, new TraverseOptions(key));
1931
+ const compiled = _JarenValidator.#compileSchema(this, origin, map);
1932
+ this.#metaSchemas.set(origin, compiled);
1933
+ mapMerge(this.#schemas, map);
1934
+ } else if (isBoolOrObjectClass(schema)) {
1935
+ const { origin, map } = _JarenValidator.#traverseSchema(schema, void 0, void 0, new TraverseOptions(key));
1936
+ const compiled = _JarenValidator.#compileSchema(this, origin, map);
1937
+ this.#metaSchemas.set(origin, compiled);
1938
+ mapMerge(this.#schemas, map);
1939
+ }
1940
+ return this;
1941
+ }
1942
+ /**
1943
+ *
1944
+ * @param {string} key
1945
+ * @returns {object}
1946
+ */
1947
+ getSchema(key) {
1948
+ key = _JarenValidator.normalizeUriKey(key);
1949
+ return this.#schemas.get(key) || null;
1950
+ }
1951
+ /**
1952
+ * Validates schema. This method should be used to validate schemas rather than validate due to the inconsistency of uri format in JSON Schema standard.
1953
+ * By default this method is called automatically when the schema is added, so you rarely need to use it directly.
1954
+ * If schema doesn't have $schema property, it is validated against draft 6 meta-schema (option meta should not be false).
1955
+ * If schema has $schema property, then the schema with this id (that should be previously added) is used to validate passed schema.
1956
+ * Errors will be available at ajv.errors
1957
+ * @param {boolean | object} schema
1958
+ * @returns {boolean}
1959
+ */
1960
+ validateSchema(schema) {
1961
+ if (this.#metaSchemas.size == 0)
1962
+ return true;
1963
+ const schemaId = _JarenValidator.normalizeUriKey(schema.$schema || DEFAULT_SCHEMA_DRAFT);
1964
+ const metaSchema = this.#metaSchemas.get(schemaId);
1965
+ if (!metaSchema)
1966
+ return false;
1967
+ return metaSchema(schema);
1968
+ }
1969
+ /**
1970
+ * Generate validating function and cache the compiled schema for future use.
1971
+ * Note: This function does NOT return a promise. Use compileAsync instead!
1972
+ * @param {boolean | object} schema
1973
+ * @param {object[] | undefined} [schemas=undefined]
1974
+ * @returns {(data: any) => boolean}
1975
+ */
1976
+ compile(schema, schemas = void 0) {
1977
+ const { origin, map } = _JarenValidator.#traverseSchema(schema, schemas, this.#schemas, this.#options.traverse);
1978
+ return _JarenValidator.#compileSchema(this, origin, map);
1979
+ }
1980
+ /**
1981
+ * Generate validating function and cache the compiled schema for future use.
1982
+ * Note: This function returns a promise.
1983
+ * @param {boolean | object} schema
1984
+ * @returns {Promise<any>}
1985
+ */
1986
+ compileAsync(schema) {
1987
+ throw new Error("compileAsync is not implemented");
1988
+ }
1989
+ };
1796
1990
  export {
1991
+ DEFAULT_SCHEMA_DRAFT,
1992
+ JarenValidator,
1797
1993
  TraverseOptions,
1798
1994
  ValidatorOptions,
1799
1995
  compileSchemaValidator,