@voxgig/struct 0.3.7 → 0.4.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.
@@ -167,7 +167,7 @@ const SKIP = { '`$SKIP`': true };
167
167
  exports.SKIP = SKIP;
168
168
  const DELETE = { '`$DELETE`': true };
169
169
  exports.DELETE = DELETE;
170
- const R_INTEGER_KEY = /^[-0-9]+$/;
170
+ const R_INTEGER_KEY = /^(0|-?[1-9][0-9]*)$/;
171
171
  const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g; // Chars that need escaping in regexp.
172
172
  const R_QUOTES = /"/g;
173
173
  const R_DOT = /\./g;
@@ -176,6 +176,7 @@ const R_META_PATH = /^([^$]+)\$([=~])(.+)$/;
176
176
  const R_DOUBLE_DOLLAR = /\$\$/g;
177
177
  const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g;
178
178
  const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/;
179
+ const R_CHECKER_NAME = /^\$[A-Z]+$/;
179
180
  const R_BT_ESCAPE = /\$BT/g;
180
181
  const R_DS_ESCAPE = /\$DS/g;
181
182
  const R_INJECTION_PARTIAL = /`([^`]+)`/g;
@@ -359,20 +360,53 @@ function typify(value) {
359
360
  // Anything else (e.g. bigint) is considered T_any
360
361
  return T_any;
361
362
  }
362
- // Get a list element. The key should be an integer, or a string
363
- // that can parse to an integer only. Negative integers count from the end of the list.
363
+ // A list key's index: an integral number, or the string String(n) prints, else NaN.
364
+ function _listkey(key) {
365
+ if (S_number === typeof key) {
366
+ return Number.isInteger(key) ? key + 0 : NaN;
367
+ }
368
+ return S_string === typeof key && R_INTEGER_KEY.test(key) ? +key : NaN;
369
+ }
370
+ // The stored value; a plain map yields own properties only, never __proto__.
371
+ function _readprop(val, key) {
372
+ if (islist(val)) {
373
+ const keyI = _listkey(key);
374
+ return 0 <= keyI ? val[keyI] : NONE;
375
+ }
376
+ if (S_string !== typeof key && S_number !== typeof key) {
377
+ return NONE;
378
+ }
379
+ if (0 < (T_instance & typify(val))) {
380
+ return val[key];
381
+ }
382
+ return Object.prototype.hasOwnProperty.call(val, key) ? val[key] : NONE;
383
+ }
384
+ function _setown(obj, key, val) {
385
+ if ('__proto__' === key) {
386
+ Object.defineProperty(obj, key, {
387
+ value: val,
388
+ enumerable: true,
389
+ writable: true,
390
+ configurable: true,
391
+ });
392
+ }
393
+ else {
394
+ obj[key] = val;
395
+ }
396
+ }
397
+ // Get a list element; a negative integer key counts from the end.
364
398
  function getelem(val, key, alt) {
365
399
  let out = NONE;
366
400
  if (NONE === val || NONE === key) {
367
401
  return alt;
368
402
  }
369
403
  if (islist(val)) {
370
- const nkey = parseInt(key);
371
- if (Number.isInteger(nkey) && re_test(R_INTEGER_KEY, '' + key)) {
372
- if (nkey < 0) {
373
- key = val.length + nkey;
374
- }
375
- out = val[key];
404
+ let keyI = _listkey(key);
405
+ if (keyI < 0) {
406
+ keyI = val.length + keyI;
407
+ }
408
+ if (0 <= keyI) {
409
+ out = val[keyI];
376
410
  }
377
411
  }
378
412
  // null at a slot counts as "no value" — same Group A rule as getprop.
@@ -389,7 +423,7 @@ function getprop(val, key, alt) {
389
423
  return alt;
390
424
  }
391
425
  if (isnode(val)) {
392
- out = val[key];
426
+ out = _readprop(val, key);
393
427
  }
394
428
  // JSON null at a key is treated as "no value" for the default-substitution
395
429
  // rule. This unifies cross-port behaviour: every host language conflates
@@ -409,7 +443,7 @@ function _lookup(val, key) {
409
443
  if (NONE === val || NONE === key)
410
444
  return NONE;
411
445
  if (isnode(val))
412
- return val[key];
446
+ return _readprop(val, key);
413
447
  return NONE;
414
448
  }
415
449
  // Convert different types of keys to string representation.
@@ -741,7 +775,7 @@ function jm(...kv) {
741
775
  // with value null" from "slot beyond end of kv".
742
776
  let k = i < kv.length ? kv[i] : '$KEY' + i;
743
777
  k = 'string' === typeof k ? k : stringify(k);
744
- o[k] = i + 1 < kv.length ? kv[i + 1] : null;
778
+ _setown(o, k, i + 1 < kv.length ? kv[i + 1] : null);
745
779
  }
746
780
  return o;
747
781
  }
@@ -766,11 +800,10 @@ function delprop(parent, key) {
766
800
  delete parent[key];
767
801
  }
768
802
  else if (islist(parent)) {
769
- let keyI = +key;
803
+ const keyI = _listkey(key);
770
804
  if (isNaN(keyI)) {
771
805
  return parent;
772
806
  }
773
- keyI = Math.floor(keyI);
774
807
  // Delete list element at position keyI, shifting later elements down.
775
808
  const psize = size(parent);
776
809
  if (0 <= keyI && keyI < psize) {
@@ -795,16 +828,13 @@ function setprop(parent, key, val) {
795
828
  throw new Error('struct: condensed structures are immutable - ' + 'expand() first, then modify the copy');
796
829
  }
797
830
  if (ismap(parent)) {
798
- key = S_MT + key;
799
- const pany = parent;
800
- pany[key] = val;
831
+ _setown(parent, S_MT + key, val);
801
832
  }
802
833
  else if (islist(parent)) {
803
- let keyI = +key;
834
+ const keyI = _listkey(key);
804
835
  if (isNaN(keyI)) {
805
836
  return parent;
806
837
  }
807
- keyI = Math.floor(keyI);
808
838
  // Set or append value at position keyI, or append if keyI out of bounds.
809
839
  if (0 <= keyI) {
810
840
  parent[slice(keyI, 0, size(parent) + 1)] = val;
@@ -973,13 +1003,27 @@ function setpath(store, path, val, injdef) {
973
1003
  return parent;
974
1004
  }
975
1005
  function getpath(store, path, injdef) {
976
- const parts = islist(path)
977
- ? path
978
- : 'string' === typeof path
979
- ? path.split(S_DT)
980
- : 'number' === typeof path
981
- ? [strkey(path)]
982
- : NONE;
1006
+ let parts = NONE;
1007
+ if (islist(path)) {
1008
+ parts = [];
1009
+ for (const part of path) {
1010
+ if (S_number === typeof part) {
1011
+ parts.push(S_MT + part);
1012
+ }
1013
+ else if (S_string === typeof part) {
1014
+ parts.push(part);
1015
+ }
1016
+ else {
1017
+ return NONE;
1018
+ }
1019
+ }
1020
+ }
1021
+ else if (S_string === typeof path) {
1022
+ parts = path.split(S_DT);
1023
+ }
1024
+ else if (S_number === typeof path) {
1025
+ parts = [S_MT + path];
1026
+ }
983
1027
  if (NONE === parts) {
984
1028
  return NONE;
985
1029
  }
@@ -1376,8 +1420,7 @@ const transform_PACK = (inj, _val, _ref, store) => {
1376
1420
  setprop(target, tkey, rval);
1377
1421
  return NONE;
1378
1422
  };
1379
- const transform_REF = (inj, val, _ref, store) => {
1380
- const { nodes } = inj;
1423
+ const transform_REF = (inj, _val, _ref, store) => {
1381
1424
  if (M_VAL !== inj.mode) {
1382
1425
  return NONE;
1383
1426
  }
@@ -1388,7 +1431,6 @@ const transform_REF = (inj, val, _ref, store) => {
1388
1431
  const dpath = slice(inj.path, 1);
1389
1432
  const ref = getpath(spec, refpath, {
1390
1433
  dpath,
1391
- // dparent: getpath(spec, inj.path.slice(1))
1392
1434
  dparent: getpath(spec, dpath),
1393
1435
  });
1394
1436
  let hasSubRef = false;
@@ -1400,31 +1442,20 @@ const transform_REF = (inj, val, _ref, store) => {
1400
1442
  return v;
1401
1443
  });
1402
1444
  }
1403
- const tref = clone(ref);
1404
- const cpath = slice(inj.path, -3);
1405
- const tpath = slice(inj.path, -1);
1406
- const tcur = getpath(store, cpath);
1407
- const tval = getpath(store, tpath);
1408
- let rval = NONE;
1409
- if (!hasSubRef || NONE !== tval) {
1410
- const tinj = inj.child(0, [getelem(tpath, -1)]);
1411
- tinj.path = tpath;
1412
- tinj.nodes = slice(inj.nodes, -1);
1413
- tinj.parent = getelem(nodes, -2);
1414
- tinj.val = tref;
1415
- tinj.dpath = flatten([cpath]);
1416
- tinj.dparent = tcur;
1417
- inject(tref, store, tinj);
1418
- rval = tinj.val;
1419
- }
1420
- else {
1421
- rval = NONE;
1422
- }
1445
+ // A recursive reference stops where the data does.
1446
+ const tval = getpath(store, slice(inj.path, -1));
1447
+ const rval = !hasSubRef || NONE !== tval ? clone(ref) : NONE;
1448
+ // The enclosing loop injects the referenced spec in this list's place, so
1449
+ // neither this pass nor this element leaves a value for modify to check.
1423
1450
  const grandparent = inj.setval(rval, 2);
1424
- if (islist(grandparent) && inj.prior) {
1425
- inj.prior.keyI--;
1451
+ const prior = inj.prior;
1452
+ if (prior) {
1453
+ prior.modify = NONE;
1454
+ if (islist(grandparent) || NONE !== rval) {
1455
+ prior.keyI--;
1456
+ }
1426
1457
  }
1427
- return val;
1458
+ return SKIP;
1428
1459
  };
1429
1460
  const transform_FORMAT = (inj, _val, _ref, store) => {
1430
1461
  // Remove remaining keys to avoid spurious processing.
@@ -1571,7 +1602,8 @@ const validate_STRING = (inj) => {
1571
1602
  };
1572
1603
  const validate_TYPE = (inj, _val, ref) => {
1573
1604
  const tname = slice(ref, 1).toLowerCase();
1574
- const typev = 1 << (31 - TYPENAME.indexOf(tname));
1605
+ // $NIL is absent or null.
1606
+ const typev = (1 << (31 - TYPENAME.indexOf(tname))) | (S_nil === tname ? T_null : 0);
1575
1607
  const out = _lookup(inj.dparent, inj.key);
1576
1608
  const t = typify(out);
1577
1609
  // console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
@@ -1599,7 +1631,7 @@ const validate_CHILD = (inj) => {
1599
1631
  tval = {};
1600
1632
  }
1601
1633
  else if (!ismap(tval)) {
1602
- inj.errs.push(_invalidTypeMsg(slice(inj.path, -1), S_object, typify(tval), tval), 'V0220');
1634
+ // _validation reports the mismatch, once.
1603
1635
  return NONE;
1604
1636
  }
1605
1637
  const ckeys = keysof(tval);
@@ -1622,8 +1654,6 @@ const validate_CHILD = (inj) => {
1622
1654
  return NONE;
1623
1655
  }
1624
1656
  if (!islist(inj.dparent)) {
1625
- const msg = _invalidTypeMsg(slice(inj.path, -1), S_list, typify(inj.dparent), inj.dparent, 'V0230');
1626
- inj.errs.push(msg);
1627
1657
  inj.keyI = size(parent);
1628
1658
  return inj.dparent;
1629
1659
  }
@@ -1647,48 +1677,6 @@ const validate_CHILD = (inj) => {
1647
1677
  }
1648
1678
  return NONE;
1649
1679
  };
1650
- const validate_ONE = (inj, _val, _ref, store) => {
1651
- const { mode, parent, keyI } = inj;
1652
- // Only operate in val mode, since parent is a list.
1653
- if (M_VAL === mode) {
1654
- if (!islist(parent) || 0 !== keyI) {
1655
- inj.errs.push('The $ONE validator at field ' +
1656
- pathify(inj.path, 1, 1) +
1657
- ' must be the first element of an array.');
1658
- return;
1659
- }
1660
- inj.keyI = size(inj.keys);
1661
- // Clean up structure, replacing [$ONE, ...] with current
1662
- inj.setval(inj.dparent, 2);
1663
- // Path shortens for the message only: `modify` reads
1664
- // getprop(inj.parent, inj.key), and inj.parent is still this list.
1665
- inj.path = slice(inj.path, -1);
1666
- const tvals = slice(parent, 1);
1667
- if (0 === size(tvals)) {
1668
- inj.errs.push('The $ONE validator at field ' +
1669
- pathify(inj.path, 1, 1) +
1670
- ' must have at least one argument.');
1671
- return;
1672
- }
1673
- for (const tval of tvals) {
1674
- const terrs = [];
1675
- const vstore = merge([{}, store], 1);
1676
- vstore.$TOP = inj.dparent;
1677
- const vcurrent = validate(inj.dparent, tval, {
1678
- extra: vstore,
1679
- errs: terrs,
1680
- meta: inj.meta,
1681
- });
1682
- inj.setval(vcurrent, -2);
1683
- // Accept current value if there was a match
1684
- if (0 === size(terrs)) {
1685
- return;
1686
- }
1687
- }
1688
- const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
1689
- inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(tvals) ? 'one of ' : '') + valdesc, typify(inj.dparent), inj.dparent, 'V0210'));
1690
- }
1691
- };
1692
1680
  const validate_EXACT = (inj) => {
1693
1681
  const { mode, parent, key, keyI } = inj;
1694
1682
  // Only operate in val mode, since parent is a list.
@@ -1699,40 +1687,231 @@ const validate_EXACT = (inj) => {
1699
1687
  ' must be the first element of an array.');
1700
1688
  return;
1701
1689
  }
1690
+ const cval = _slotval(inj);
1702
1691
  inj.keyI = size(inj.keys);
1703
- // Clean up structure, replacing [$EXACT, ...] with current data parent
1704
- inj.setval(inj.dparent, 2);
1692
+ // Replace [$EXACT, ...] with the data value; SKIP keeps inject off the detached list.
1693
+ inj.setval(cval, 2);
1705
1694
  inj.path = slice(inj.path, 0, -1);
1706
- inj.key = getelem(inj.path, -1);
1707
1695
  const tvals = slice(parent, 1);
1708
1696
  if (0 === size(tvals)) {
1709
1697
  inj.errs.push('The $EXACT validator at field ' +
1710
1698
  pathify(inj.path, 1, 1) +
1711
1699
  ' must have at least one argument.');
1712
- return;
1700
+ return SKIP;
1713
1701
  }
1714
- let currentstr = undefined;
1715
1702
  for (const tval of tvals) {
1716
- let exactmatch = tval === inj.dparent;
1717
- if (!exactmatch && isnode(tval)) {
1718
- currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
1719
- const tvalstr = stringify(tval);
1720
- exactmatch = tvalstr === currentstr;
1721
- }
1722
- if (exactmatch) {
1723
- return;
1703
+ if (_deepequal(tval, cval)) {
1704
+ return SKIP;
1724
1705
  }
1725
1706
  }
1726
1707
  const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
1727
1708
  inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(inj.path) ? '' : 'value ') +
1728
1709
  'exactly equal to ' +
1729
1710
  (1 === size(tvals) ? '' : 'one of ') +
1730
- valdesc, typify(inj.dparent), inj.dparent, 'V0110'));
1711
+ valdesc, typify(cval), cval, 'V0110'));
1712
+ return SKIP;
1731
1713
  }
1732
1714
  else {
1733
1715
  delprop(parent, key);
1734
1716
  }
1735
1717
  };
1718
+ // Count the specs that admit the value: ['`$NOF`', count, spec, ...], where
1719
+ // count is an integer, or a map holding min, max or both.
1720
+ const validate_NOF = (inj, _val, _ref, store) => _validatecount(inj, store, '$NOF', true);
1721
+ // Every spec admits the value: ['`$ALL`', spec, ...].
1722
+ const validate_ALL = (inj, _val, _ref, store) => _validatecount(inj, store, '$ALL', false);
1723
+ // The counting list directives. Without a count, every spec has to admit.
1724
+ function _validatecount(inj, store, name, counted) {
1725
+ const { mode, parent, keyI } = inj;
1726
+ if (M_VAL !== mode) {
1727
+ return NONE;
1728
+ }
1729
+ if (!islist(parent) || 0 !== keyI) {
1730
+ inj.errs.push('The ' +
1731
+ name +
1732
+ ' validator at field ' +
1733
+ pathify(inj.path, 1, 1) +
1734
+ ' must be the first element of an array.');
1735
+ return NONE;
1736
+ }
1737
+ const cval = _slotval(inj);
1738
+ inj.keyI = size(inj.keys);
1739
+ // Replace the directive's list with the data value. Each return is SKIP, so
1740
+ // that inject leaves the detached list alone and validates nothing in it.
1741
+ inj.setval(cval, 2);
1742
+ inj.path = slice(inj.path, -1);
1743
+ const count = counted ? _lookup(parent, 1) : NONE;
1744
+ const tvals = slice(parent, counted ? 2 : 1);
1745
+ const numtvals = size(tvals);
1746
+ const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1);
1747
+ if (0 === numtvals) {
1748
+ inj.errs.push(field + (counted ? ' must have a count and' : ' must have') + ' at least one argument.');
1749
+ return SKIP;
1750
+ }
1751
+ const [lo, hi, countdesc] = counted ? _nofcount(count, numtvals) : [numtvals, numtvals, 'all'];
1752
+ if (NONE === countdesc) {
1753
+ inj.errs.push(field + ' has an invalid count: ' + stringify(count) + '.');
1754
+ return SKIP;
1755
+ }
1756
+ let admitted = 0;
1757
+ for (let tI = 0; tI < numtvals; tI++) {
1758
+ const left = numtvals - tI;
1759
+ // Stop once the specs left cannot change the verdict.
1760
+ if (hi < admitted || admitted + left < lo || (lo <= admitted && admitted + left <= hi)) {
1761
+ break;
1762
+ }
1763
+ if (_admits(cval, tvals[tI], inj, store)) {
1764
+ admitted++;
1765
+ }
1766
+ }
1767
+ if (admitted < lo || hi < admitted) {
1768
+ const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
1769
+ inj.errs.push(_invalidTypeMsg(inj.path, countdesc + ' of ' + valdesc, typify(cval), cval, 'V0310'));
1770
+ }
1771
+ return SKIP;
1772
+ }
1773
+ // The value's length is within a bound: ['`$LEN`', bound, spec?], where the
1774
+ // bound is as for a $NOF count. A string's length is in code points.
1775
+ const validate_LEN = (inj, _val, _ref, store) => _validatebound(inj, store, '$LEN');
1776
+ // The value is a number within a bound: ['`$RANGE`', bound, spec?], where the
1777
+ // bound holds min or gt, max or lt, or one of each.
1778
+ const validate_RANGE = (inj, _val, _ref, store) => _validatebound(inj, store, '$RANGE');
1779
+ const validate_LIKE = (inj, _val, _ref, store) => _validatebound(inj, store, '$LIKE');
1780
+ // The bounding list directives: a bound or pattern, then at most one spec that also admits.
1781
+ function _validatebound(inj, store, name) {
1782
+ const { mode, parent, keyI } = inj;
1783
+ if (M_VAL !== mode) {
1784
+ return NONE;
1785
+ }
1786
+ if (!islist(parent) || 0 !== keyI) {
1787
+ inj.errs.push('The ' +
1788
+ name +
1789
+ ' validator at field ' +
1790
+ pathify(inj.path, 1, 1) +
1791
+ ' must be the first element of an array.');
1792
+ return NONE;
1793
+ }
1794
+ const cval = _slotval(inj);
1795
+ inj.keyI = size(inj.keys);
1796
+ // As for the counting directives: SKIP keeps inject off the detached list.
1797
+ inj.setval(cval, 2);
1798
+ inj.path = slice(inj.path, -1);
1799
+ const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1);
1800
+ const nargs = size(parent) - 1;
1801
+ const islen = '$LEN' === name;
1802
+ const islike = '$LIKE' === name;
1803
+ const argname = islike ? 'pattern' : 'bound';
1804
+ if (nargs < 1 || 2 < nargs) {
1805
+ inj.errs.push(field + ' must have a ' + argname + ' and at most one spec.');
1806
+ return SKIP;
1807
+ }
1808
+ const bound = _lookup(parent, 1);
1809
+ const like = islike ? _likere(bound) : NONE;
1810
+ const [lo, hi, desc] = islen
1811
+ ? _lenbound(bound)
1812
+ : [0, 0, islike ? (NONE === like ? NONE : 'like /' + bound + '/') : _rangedesc(bound)];
1813
+ if (NONE === desc) {
1814
+ inj.errs.push(field + ' has an invalid ' + argname + ': ' + stringify(bound) + '.');
1815
+ return SKIP;
1816
+ }
1817
+ const ctype = typify(cval);
1818
+ if (2 === nargs && !_admits(cval, _lookup(parent, 2), inj, store)) {
1819
+ const specdesc = replace(stringify(_lookup(parent, 2)), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
1820
+ inj.errs.push(_invalidTypeMsg(inj.path, specdesc, ctype, cval, 'V0410'));
1821
+ return SKIP;
1822
+ }
1823
+ if (0 === ((islen ? T_string | T_list | T_map : islike ? T_string : T_number) & ctype)) {
1824
+ const needtype = islen ? 'string, list or map' : islike ? S_string : S_number;
1825
+ inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0420'));
1826
+ return SKIP;
1827
+ }
1828
+ const len = islen
1829
+ ? S_string === typeof cval
1830
+ ? Array.from(cval).length
1831
+ : size(cval)
1832
+ : 0;
1833
+ const inbound = islen
1834
+ ? lo <= len && (-1 === hi || len <= hi)
1835
+ : islike
1836
+ ? re_test(like, cval)
1837
+ : _inrange(bound, cval);
1838
+ if (!inbound) {
1839
+ const needtype = (1 < size(inj.path) ? '' : 'value ') + desc;
1840
+ inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0430'));
1841
+ }
1842
+ return SKIP;
1843
+ }
1844
+ // The bounds of a $LEN bound, with -1 for no maximum, and the words for it,
1845
+ // which are absent when the bound is invalid.
1846
+ function _lenbound(bound) {
1847
+ const [lo, hi, countdesc] = _nofcount(bound, -1);
1848
+ if (NONE === countdesc) {
1849
+ return [0, 0, NONE];
1850
+ }
1851
+ const desc = 'of length ' +
1852
+ (lo === hi ? lo : -1 === hi ? 'at least ' + lo : 0 === lo ? 'at most ' + hi : lo + ' to ' + hi);
1853
+ return [lo, hi, desc];
1854
+ }
1855
+ // A $LIKE pattern compiled by the port's own regex engine; absent unless it compiles.
1856
+ function _likere(pattern) {
1857
+ if (S_string !== typeof pattern) {
1858
+ return NONE;
1859
+ }
1860
+ try {
1861
+ return re_compile(pattern);
1862
+ }
1863
+ catch {
1864
+ return NONE;
1865
+ }
1866
+ }
1867
+ // The words for a $RANGE bound, which are absent when the bound is invalid.
1868
+ function _rangedesc(bound) {
1869
+ if (!ismap(bound)) {
1870
+ return NONE;
1871
+ }
1872
+ const min = _lookup(bound, 'min');
1873
+ const max = _lookup(bound, 'max');
1874
+ const gt = _lookup(bound, 'gt');
1875
+ const lt = _lookup(bound, 'lt');
1876
+ const lower = NONE === min ? gt : min;
1877
+ const upper = NONE === max ? lt : max;
1878
+ const isnum = (n) => NONE === n || 0 < (T_number & typify(n));
1879
+ const valid = 0 < size(bound) &&
1880
+ size(bound) === [min, max, gt, lt].filter((n) => NONE !== n).length &&
1881
+ (NONE === min || NONE === gt) &&
1882
+ (NONE === max || NONE === lt) &&
1883
+ isnum(min) &&
1884
+ isnum(max) &&
1885
+ isnum(gt) &&
1886
+ isnum(lt) &&
1887
+ (NONE === lower || NONE === upper || lower <= upper);
1888
+ if (!valid) {
1889
+ return NONE;
1890
+ }
1891
+ return join([
1892
+ NONE !== min
1893
+ ? 'at least ' + stringify(min)
1894
+ : NONE !== gt
1895
+ ? 'greater than ' + stringify(gt)
1896
+ : NONE,
1897
+ NONE !== max
1898
+ ? 'at most ' + stringify(max)
1899
+ : NONE !== lt
1900
+ ? 'less than ' + stringify(lt)
1901
+ : NONE,
1902
+ ], ' and ');
1903
+ }
1904
+ // A number within a valid $RANGE bound.
1905
+ function _inrange(bound, val) {
1906
+ const min = _lookup(bound, 'min');
1907
+ const max = _lookup(bound, 'max');
1908
+ const gt = _lookup(bound, 'gt');
1909
+ const lt = _lookup(bound, 'lt');
1910
+ return ((NONE === min || min <= val) &&
1911
+ (NONE === max || val <= max) &&
1912
+ (NONE === gt || gt < val) &&
1913
+ (NONE === lt || val < lt));
1914
+ }
1736
1915
  // This is the "modify" argument to inject. Use this to perform
1737
1916
  // generic validation. Runs *after* any special commands.
1738
1917
  const _validation = (pval, key, parent, inj) => {
@@ -1834,8 +2013,12 @@ injdef) {
1834
2013
  $INSTANCE: validate_TYPE,
1835
2014
  $ANY: validate_ANY,
1836
2015
  $CHILD: validate_CHILD,
1837
- $ONE: validate_ONE,
1838
2016
  $EXACT: validate_EXACT,
2017
+ $NOF: validate_NOF,
2018
+ $ALL: validate_ALL,
2019
+ $LEN: validate_LEN,
2020
+ $RANGE: validate_RANGE,
2021
+ $LIKE: validate_LIKE,
1839
2022
  },
1840
2023
  getdef(extra, {}),
1841
2024
  // A special top level value to collect errors.
@@ -1846,13 +2029,18 @@ injdef) {
1846
2029
  ], 1);
1847
2030
  const meta = getprop(injdef, 'meta', {});
1848
2031
  setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false));
1849
- const out = transform(data, spec, {
1850
- meta,
1851
- extra: store,
1852
- modify: _validation,
1853
- handler: _validatehandler,
1854
- errs,
1855
- });
2032
+ // A spec that names an unknown checker validates nothing.
2033
+ const numerrs = size(errs);
2034
+ _checknames(spec, store, [], errs);
2035
+ const out = numerrs < size(errs)
2036
+ ? clone(data)
2037
+ : transform(data, spec, {
2038
+ meta,
2039
+ extra: store,
2040
+ modify: _validation,
2041
+ handler: _validatehandler,
2042
+ errs,
2043
+ });
1856
2044
  const generr = 0 < size(errs) && !collect;
1857
2045
  if (generr) {
1858
2046
  throw new Error(join(errs, ' | '));
@@ -1928,6 +2116,10 @@ const select_NOT = (inj, _val, _ref, store) => {
1928
2116
  }
1929
2117
  };
1930
2118
  const select_CMP = (inj, _val, ref, store) => {
2119
+ // The list form of $LIKE is validate's checker; the key form is this operator.
2120
+ if (M_VAL === inj.mode && '$LIKE' === ref) {
2121
+ return validate_LIKE(inj, _val, ref, store);
2122
+ }
1931
2123
  if (M_KEYPRE === inj.mode) {
1932
2124
  const term = _lookup(inj.parent, inj.key);
1933
2125
  const gkey = getelem(inj.path, -2);
@@ -1996,12 +2188,7 @@ function select(children, query) {
1996
2188
  },
1997
2189
  };
1998
2190
  const q = clone(query);
1999
- walk(q, (_k, v) => {
2000
- if (ismap(v)) {
2001
- setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true));
2002
- }
2003
- return v;
2004
- });
2191
+ _openquery(q);
2005
2192
  for (const child of children) {
2006
2193
  injdef.errs = [];
2007
2194
  validate(child, clone(q), injdef);
@@ -2011,6 +2198,27 @@ function select(children, query) {
2011
2198
  }
2012
2199
  return results;
2013
2200
  }
2201
+ // Open every map in a select query, so a child may hold keys the query does
2202
+ // not name, but leave a directive's literals as written: what $EXACT
2203
+ // compares, and the count or bound of $NOF, $LEN and $RANGE.
2204
+ function _openquery(q) {
2205
+ if (ismap(q)) {
2206
+ setprop(q, '`$OPEN`', getprop(q, '`$OPEN`', true));
2207
+ for (const key of keysof(q)) {
2208
+ _openquery(q[key]);
2209
+ }
2210
+ }
2211
+ else if (islist(q)) {
2212
+ const head = q[0];
2213
+ const exact = '`$EXACT`' === head;
2214
+ const bounded = '`$NOF`' === head || '`$LEN`' === head || '`$RANGE`' === head;
2215
+ for (let i = 0; i < size(q); i++) {
2216
+ if (0 === i || !(exact || (bounded && 1 === i))) {
2217
+ _openquery(q[i]);
2218
+ }
2219
+ }
2220
+ }
2221
+ }
2014
2222
  // Injection state used for recursive injection into JSON - like data structures.
2015
2223
  class Injection {
2016
2224
  constructor(val, parent) {
@@ -2134,6 +2342,137 @@ function _invalidTypeMsg(path, needtype, vt, v, _whence) {
2134
2342
  // ' [' + _whence + ']' +
2135
2343
  '.');
2136
2344
  }
2345
+ // Names a validate spec may use besides its checkers: transform's that it keeps, and $OPEN.
2346
+ const VALIDATE_NAMES = [
2347
+ '$TOP',
2348
+ '$SPEC',
2349
+ '$BT',
2350
+ '$DS',
2351
+ '$WHEN',
2352
+ '$ANNO',
2353
+ '$REF',
2354
+ '$FORMAT',
2355
+ '$APPLY',
2356
+ '$OPEN',
2357
+ ];
2358
+ // Report each checker the spec names that validate lacks; $EXACT's arguments are literals.
2359
+ function _checknames(spec, store, path, errs) {
2360
+ const name = _refname(spec);
2361
+ if (NONE !== name) {
2362
+ if (_unknownname(name, store)) {
2363
+ errs.push('Unknown validator ' + name + ' at field ' + pathify(path) + '.');
2364
+ }
2365
+ return;
2366
+ }
2367
+ if (islist(spec) && '$EXACT' === _refname(spec[0])) {
2368
+ return;
2369
+ }
2370
+ for (const key of keysof(spec)) {
2371
+ if (ismap(spec)) {
2372
+ _checknames(key, store, path, errs);
2373
+ }
2374
+ _checknames(spec[key], store, flatten([path, key]), errs);
2375
+ }
2376
+ }
2377
+ // The name a whole-string reference gives, when it is a checker name in any case.
2378
+ function _refname(val) {
2379
+ const m = S_string === typeof val ? re_find(R_INJECTION_FULL, val) : null;
2380
+ return null != m && re_test(R_CHECKER_NAME, m[1].toUpperCase()) ? m[1] : NONE;
2381
+ }
2382
+ // Unknown: undefined and upper case, or a defined name in another case. Null disables a name.
2383
+ function _unknownname(name, store) {
2384
+ const known = (n) => VALIDATE_NAMES.includes(n) || null != _lookup(store, n);
2385
+ const upper = name.toUpperCase();
2386
+ return !known(name) && (upper === name || known(upper));
2387
+ }
2388
+ // The data value at a list directive's slot. The directive's own dparent was
2389
+ // read with getprop, which reports a stored null as absent.
2390
+ function _slotval(inj) {
2391
+ const prior = inj.prior;
2392
+ return NONE === prior ? inj.dparent : _lookup(prior.dparent, prior.key);
2393
+ }
2394
+ // JSON equality: numbers by value, lists in order, maps by key in any order.
2395
+ function _deepequal(a, b) {
2396
+ if (a === b) {
2397
+ return true;
2398
+ }
2399
+ const atype = typify(a);
2400
+ if (atype !== typify(b) || 0 === ((T_list | T_map) & atype)) {
2401
+ return false;
2402
+ }
2403
+ const akeys = keysof(a);
2404
+ const bkeys = keysof(b);
2405
+ if (size(akeys) !== size(bkeys)) {
2406
+ return false;
2407
+ }
2408
+ for (let kI = 0; kI < size(akeys); kI++) {
2409
+ const k = akeys[kI];
2410
+ if (k !== bkeys[kI] || !_deepequal(a[k], b[k])) {
2411
+ return false;
2412
+ }
2413
+ }
2414
+ return true;
2415
+ }
2416
+ // Every member val has is in out, unchanged. A member only out has is a
2417
+ // default the spec supplied, and is ignored.
2418
+ function _keeps(val, out) {
2419
+ if (NONE === val) {
2420
+ return true;
2421
+ }
2422
+ if (0 === ((T_list | T_map) & typify(val)) || islist(val) !== islist(out) || !isnode(out)) {
2423
+ return _deepequal(val, out);
2424
+ }
2425
+ for (const k of keysof(val)) {
2426
+ if (!_keeps(val[k], _lookup(out, k))) {
2427
+ return false;
2428
+ }
2429
+ }
2430
+ return true;
2431
+ }
2432
+ // A spec admits a value when validating the value against it finds no error
2433
+ // and gives back the value as it is.
2434
+ function _admits(val, spec, inj, store) {
2435
+ const terrs = [];
2436
+ const vstore = merge([{}, store], 1);
2437
+ vstore.$TOP = val;
2438
+ const out = validate(val, spec, {
2439
+ extra: vstore,
2440
+ errs: terrs,
2441
+ meta: inj.meta,
2442
+ });
2443
+ return 0 === size(terrs) && _keeps(val, out);
2444
+ }
2445
+ // The bounds of a $NOF count, and the words for it, which are absent when
2446
+ // the count is invalid. Without a max, the bound is the number of specs.
2447
+ function _nofcount(count, numtvals) {
2448
+ const isnat = (n) => 0 < (T_integer & typify(n)) && 0 <= n;
2449
+ if (isnat(count)) {
2450
+ return [count, count, 0 === count ? 'none' : 'exactly ' + count];
2451
+ }
2452
+ const min = _lookup(count, 'min');
2453
+ const max = _lookup(count, 'max');
2454
+ const valid = ismap(count) &&
2455
+ 0 < size(count) &&
2456
+ size(count) === (NONE === min ? 0 : 1) + (NONE === max ? 0 : 1) &&
2457
+ (NONE === min || isnat(min)) &&
2458
+ (NONE === max || isnat(max)) &&
2459
+ (NONE === min || NONE === max || min <= max);
2460
+ if (!valid) {
2461
+ return [0, 0, NONE];
2462
+ }
2463
+ const lo = NONE === min ? 0 : min;
2464
+ const hi = NONE === max ? numtvals : max;
2465
+ const desc = NONE === max
2466
+ ? 'at least ' + lo
2467
+ : 0 === hi
2468
+ ? 'none'
2469
+ : NONE === min
2470
+ ? 'at most ' + hi
2471
+ : lo === hi
2472
+ ? 'exactly ' + lo
2473
+ : lo + ' to ' + hi;
2474
+ return [lo, hi, desc];
2475
+ }
2137
2476
  // Default inject handler for transforms. If the path resolves to a function,
2138
2477
  // call the function passing the injection inj. This is how transforms operate.
2139
2478
  const _injecthandler = (inj, val, ref, store) => {
@@ -2388,19 +2727,6 @@ function condenseCmp(a, b) {
2388
2727
  }
2389
2728
  return a.length - i - (b.length - j);
2390
2729
  }
2391
- function condenseSet(obj, key, val) {
2392
- if ('__proto__' === key) {
2393
- Object.defineProperty(obj, key, {
2394
- value: val,
2395
- enumerable: true,
2396
- writable: true,
2397
- configurable: true,
2398
- });
2399
- }
2400
- else {
2401
- obj[key] = val;
2402
- }
2403
- }
2404
2730
  function iscondensed(val) {
2405
2731
  // The version must MATCH, not merely be numeric: a future profile would
2406
2732
  // otherwise be decoded under this one's schema.
@@ -2459,8 +2785,8 @@ function condense(val) {
2459
2785
  const build = (n) => {
2460
2786
  if (ismap(n)) {
2461
2787
  const enc = [CONDENSE_MAP];
2462
- // keysof sorts, so the key ids ascend and lookup can bisect.
2463
- const keys = keysof(n);
2788
+ // Code-point order, not keysof's code-unit order, so the key ids ascend for bisection.
2789
+ const keys = keysof(n).sort(condenseCmp);
2464
2790
  for (let i = 0; i < keys.length; i++) {
2465
2791
  enc.push(symid[keys[i]]);
2466
2792
  enc.push(build(n[keys[i]]));
@@ -2520,7 +2846,7 @@ function condenseSymId(cond, key) {
2520
2846
  // The node index of `key` inside node `idx`, or -1. Touches only this node.
2521
2847
  function condenseChild(cond, idx, key) {
2522
2848
  const enc = cond[S_condnode][idx];
2523
- if (null == enc) {
2849
+ if (null == enc || (S_string !== typeof key && S_number !== typeof key)) {
2524
2850
  return -1;
2525
2851
  }
2526
2852
  if (CONDENSE_MAP === enc[0]) {
@@ -2597,7 +2923,7 @@ function condenseMaterialise(cond, idx) {
2597
2923
  if (CONDENSE_MAP === kind) {
2598
2924
  const out = {};
2599
2925
  for (let j = 1; j < enc.length; j += 2) {
2600
- condenseSet(out, sym[enc[j]], rebuild(enc[j + 1]));
2926
+ _setown(out, sym[enc[j]], rebuild(enc[j + 1]));
2601
2927
  }
2602
2928
  return out;
2603
2929
  }