ata-validator 1.59.0 → 1.61.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.
package/lib/aot-impl.js CHANGED
@@ -367,7 +367,7 @@ function defaultsDecl(validator) {
367
367
  if (defaults.buildDefaultsApplier(schema) === null) return '';
368
368
  const embedded = require('./schema-strip').stripAnnotationsKeepingDefaults(schema);
369
369
  const text = JSON.stringify(JSON.stringify(embedded)).replace(/</g, '\\u003c');
370
- return `const _ataDefaults = (function () {\nconst MAX_DEPTH = ${defaults.MAX_DEPTH};\n${require(AOT_ORDER_MODULE).take('defaults.js', defaults.EMBED)}\nreturn buildDefaultsApplier;\n})();\n` +
370
+ return `const _ataDefaults = (function () {\nconst MAX_DEPTH = ${defaults.MAX_DEPTH};\n${loadAotOrder().take('defaults.js', defaults.EMBED)}\nreturn buildDefaultsApplier;\n})();\n` +
371
371
  `let _ataFill;\nfunction applyDefaults(d) { (_ataFill || (_ataFill = _ataDefaults(JSON.parse(${text}))))(d); }\n`;
372
372
  }
373
373
 
@@ -379,20 +379,20 @@ function defaultsDecl(validator) {
379
379
  // the runtime accepts. Where the generated pass declines and the runtime
380
380
  // would change the data with its closure pass, there is nothing to carry it
381
381
  // in, and the bundle refuses. `useDefaults` alone runs applyDefaults.
382
- // Returns { decl, call }.
383
- function bundlePreprocess(validator, opts, fillDecl, label) {
382
+ // Returns { decl, call }, the call naming the data parameter `arg`.
383
+ function bundlePreprocess(validator, opts, fillDecl, label, arg = 'd') {
384
384
  const coerceTypes = opts && opts.coerceTypes ? opts.coerceTypes : false;
385
385
  const removeAdditional = opts && opts.removeAdditional ? opts.removeAdditional : false;
386
386
  const useDefaults = !!(opts && opts.useDefaults === true);
387
- if (!coerceTypes && !removeAdditional) return { decl: '', call: useDefaults && fillDecl ? 'applyDefaults(d);' : '' };
387
+ if (!coerceTypes && !removeAdditional) return { decl: '', call: useDefaults && fillDecl ? `applyDefaults(${arg});` : '' };
388
388
  const internals = require('./validator-core')._internals;
389
389
  const schema = internals.resolveSchemaForPreprocess(validator._schemaObj, validator._schemaMap);
390
390
  let src = null;
391
391
  try { src = require('./codegen-engine').preprocessSource(schema, { coerceTypes, removeAdditional, useDefaults }); } catch { src = null; }
392
- if (src !== null) return { decl: `function _ataPre(d) {\n${src}\n}\n`, call: '_ataPre(d);' };
392
+ if (src !== null) return { decl: `function _ataPre(d) {\n${src}\n}\n`, call: `_ataPre(${arg});` };
393
393
  const changes = (coerceTypes && internals.buildCoercer(schema) !== null) || (removeAdditional && internals.buildRemover(schema) !== null);
394
394
  if (changes) throw new Error(`${label}: ${coerceTypes ? 'coerceTypes' : 'removeAdditional'} cannot be compiled into a standalone module for this schema; validate with the runtime Validator`);
395
- return { decl: '', call: useDefaults && fillDecl ? 'applyDefaults(d);' : '' };
395
+ return { decl: '', call: useDefaults && fillDecl ? `applyDefaults(${arg});` : '' };
396
396
  }
397
397
 
398
398
  function toStandaloneModule(validator, opts) {
@@ -408,6 +408,12 @@ function toStandaloneModule(validator, opts) {
408
408
  const source = !!(opts && opts.source);
409
409
  const sourceMap = opts && opts.sourceMap ? opts.sourceMap : null;
410
410
  const schemaFile = opts && opts.schemaFile ? opts.schemaFile : null;
411
+ // The validator's own coerceTypes and removeAdditional run before every
412
+ // check, as they do in the validator; a module used to leave them out and
413
+ // reject what the validator accepts. Defaults are not filled here: a module
414
+ // exports applyDefaults for that, and validate() leaves the data as it is.
415
+ const vopts = validator._options || {};
416
+ const pre = bundlePreprocess(validator, { coerceTypes: vopts.coerceTypes, removeAdditional: vopts.removeAdditional }, '', 'toStandaloneModule', 'data');
411
417
  const src = jsFn._source;
412
418
 
413
419
  let errCore = '';
@@ -452,7 +458,7 @@ function toStandaloneModule(validator, opts) {
452
458
  // sorts by each error site's ordinal, the collector writes it (`_o`).
453
459
  const schemaForErrors = typeof validator._schemaObj === 'object' ? validator._schemaObj : {};
454
460
  ordered = !!(opts && opts.order === true);
455
- const noOrdinal = !ordered || !require(AOT_ORDER_MODULE).wantsOrdinals(schemaForErrors);
461
+ const noOrdinal = !ordered || !loadAotOrder().wantsOrdinals(schemaForErrors);
456
462
  jsErrFn = compileToJSCodegenWithErrors(
457
463
  schemaForErrors,
458
464
  null,
@@ -594,7 +600,7 @@ function toStandaloneModule(validator, opts) {
594
600
  compactCore = `let _ataErrs = null;\nfunction _ataErrors(d) { if (_ataErrs === null) _ataErrs = _ataCreateErrors(JSON.parse(${text})${rtOpts ? ', { ' + rtOpts + ' }' : ''}); return _ataErrs(d); }\n`;
595
601
  }
596
602
  const orderDecl = errCore && ordered && errorRuntime === null && onePass === null
597
- ? require(AOT_ORDER_MODULE).orderDecl(validator._schemaObj)
603
+ ? loadAotOrder().orderDecl(validator._schemaObj)
598
604
  : '';
599
605
  // Asked for and not given: stub errors, the one-pass function, or no error
600
606
  // code generated. The caller is told rather than handed the collector's
@@ -636,7 +642,7 @@ function toStandaloneModule(validator, opts) {
636
642
  // reading patterns. A second declaration of that name shadowed it and the
637
643
  // prelude called this one instead, reaching _fn before its initialiser ran.
638
644
  const parseCore = cloneExpr
639
- ? `function _ataParse(data) {\n if (!_fn(data)) { const e = new Error('validation failed'); e.name = 'AtaValidationError'; throw e; }\n return ${cloneExpr};\n}\n`
645
+ ? `function _ataParse(data) {\n ${pre.call}if (!_fn(data)) { const e = new Error('validation failed'); e.name = 'AtaValidationError'; throw e; }\n return ${cloneExpr};\n}\n`
640
646
  : '';
641
647
 
642
648
  // validateJSON(): parse the text, validate, and on failure attach a
@@ -725,25 +731,20 @@ const ABORT = Object.freeze({
725
731
  ${closureDecls}${sharedBlock}${preambleDecls}${formatDecls}const _fn = function(d) {
726
732
  ${src}
727
733
  };
728
- ${errCore}${orderDecl}${compactCore}function isValid(data) { return _fn(data); }
729
- function validate(data) { ${validBody}; }
734
+ ${errCore}${orderDecl}${compactCore}${pre.decl}function isValid(data) { ${pre.call}return _fn(data); }
735
+ function validate(data) { ${pre.call}${validBody}; }
730
736
  ${parseCore}${positionsCore}${fillDecl}${hashDecl}${exports}`;
731
737
  }
732
738
 
733
- // Why a validator cannot be written as a compact module, or null when it can.
734
- // The error runtime walks the embedded schema with the interpreted engine and
735
- // knows nothing the schema does not say, so anything the validator adds from
736
- // outside the schema keeps the module self-contained.
737
- // The error runtime's generator reads the library's own files with fs, and
738
- // runs in Node only (ata build, errorRuntimeSource). It is required through a
739
- // name rather than a literal so that a bundler following this module, in a
740
- // build of the package for a runtime without fs, does not try to bring fs
741
- // along: 1.49.0 bundled for such runtimes, and the generator must not break
742
- // that.
743
- const ERROR_RUNTIME_MODULE = './error-runtime';
744
- // Reads the library's files too, at build time (see lib/aot-order.js).
745
- const AOT_ORDER_MODULE = './aot-order';
746
- function loadErrorRuntime() { return require(ERROR_RUNTIME_MODULE); }
739
+ // The error runtime and the order helpers are required by their literal
740
+ // names: an executable compiled ahead of time (Bun) loads a module from a
741
+ // computed path by throwing, and warns about every such require at build
742
+ // time. Both modules read the library's own files with fs at build time;
743
+ // a bundle for a runtime without fs is kept away from lib/aot-order.js by
744
+ // the package's `browser` field, and lib/error-runtime.js loads fs only in
745
+ // the functions that read files.
746
+ function loadErrorRuntime() { return require('./error-runtime'); }
747
+ function loadAotOrder() { return require('./aot-order'); }
747
748
 
748
749
  // The schema hash a module stamps, once per validator: a plugin writes both
749
750
  // modules of a schema (self-contained and compact) from one validator, and
@@ -967,7 +968,7 @@ function bundleStandalone(Validator, schemas, opts) {
967
968
  v._ensureCompiled();
968
969
  const jsFn = v._jsFn;
969
970
  if (!jsFn || !jsFn._hybridSource) return null;
970
- const noOrdinal = !ordered || !require(AOT_ORDER_MODULE).wantsOrdinals(v._schemaObj);
971
+ const noOrdinal = !ordered || !loadAotOrder().wantsOrdinals(v._schemaObj);
971
972
  // The schema the validator compiled, not the one the caller passed. They
972
973
  // differ whenever anything prepared the schema: draft-07 normalization,
973
974
  // `format` removed under assertFormat: false, keywords outside the
@@ -1034,7 +1035,7 @@ function bundleStandalone(Validator, schemas, opts) {
1034
1035
  // In order: the collector's errors sorted as the runtime sorts them.
1035
1036
  const hasErr = !!(jsErrFn && jsErrFn._errSource);
1036
1037
  const errDecl = ordered && hasErr
1037
- ? `${require(AOT_ORDER_MODULE).orderDecl(v._schemaObj)}var E=function(d){var r=(function(d){var _all=true;${errBody}})(d);return r&&r.errors?{valid:false,errors:_ataOrder(r.errors)}:r};`
1038
+ ? `${loadAotOrder().orderDecl(v._schemaObj)}var E=function(d){var r=(function(d){var _all=true;${errBody}})(d);return r&&r.errors?{valid:false,errors:_ataOrder(r.errors)}:r};`
1038
1039
  : `var E=function(d){var _all=true;${errBody}};`;
1039
1040
  if (opts && opts.verbose) {
1040
1041
  // Embed the schema and a small resolver so errors carry parentSchema.