File size: 20,925 Bytes
08aee07 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | 'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Clone = require('@hapi/hoek/lib/clone');
const Ignore = require('@hapi/hoek/lib/ignore');
const Reach = require('@hapi/hoek/lib/reach');
const Common = require('./common');
const Errors = require('./errors');
const State = require('./state');
const internals = {
result: Symbol('result')
};
exports.entry = function (value, schema, prefs) {
let settings = Common.defaults;
if (prefs) {
Assert(prefs.warnings === undefined, 'Cannot override warnings preference in synchronous validation');
Assert(prefs.artifacts === undefined, 'Cannot override artifacts preference in synchronous validation');
settings = Common.preferences(Common.defaults, prefs);
}
const result = internals.entry(value, schema, settings);
Assert(!result.mainstay.externals.length, 'Schema with external rules must use validateAsync()');
const outcome = { value: result.value };
if (result.error) {
outcome.error = result.error;
}
if (result.mainstay.warnings.length) {
outcome.warning = Errors.details(result.mainstay.warnings);
}
if (result.mainstay.debug) {
outcome.debug = result.mainstay.debug;
}
if (result.mainstay.artifacts) {
outcome.artifacts = result.mainstay.artifacts;
}
return outcome;
};
exports.entryAsync = async function (value, schema, prefs) {
let settings = Common.defaults;
if (prefs) {
settings = Common.preferences(Common.defaults, prefs);
}
const result = internals.entry(value, schema, settings);
const mainstay = result.mainstay;
if (result.error) {
if (mainstay.debug) {
result.error.debug = mainstay.debug;
}
throw result.error;
}
if (mainstay.externals.length) {
let root = result.value;
const errors = [];
for (const external of mainstay.externals) {
const path = external.state.path;
const linked = external.schema.type === 'link' ? mainstay.links.get(external.schema) : null;
let node = root;
let key;
let parent;
const ancestors = path.length ? [root] : [];
const original = path.length ? Reach(value, path) : value;
if (path.length) {
key = path[path.length - 1];
let current = root;
for (const segment of path.slice(0, -1)) {
current = current[segment];
ancestors.unshift(current);
}
parent = ancestors[0];
node = parent[key];
}
try {
const createError = (code, local) => (linked || external.schema).$_createError(code, node, local, external.state, settings);
const output = await external.method(node, {
schema: external.schema,
linked,
state: external.state,
prefs,
original,
error: createError,
errorsArray: internals.errorsArray,
warn: (code, local) => mainstay.warnings.push((linked || external.schema).$_createError(code, node, local, external.state, settings)),
message: (messages, local) => (linked || external.schema).$_createError('external', node, local, external.state, settings, { messages })
});
if (output === undefined ||
output === node) {
continue;
}
if (output instanceof Errors.Report) {
mainstay.tracer.log(external.schema, external.state, 'rule', 'external', 'error');
errors.push(output);
if (settings.abortEarly) {
break;
}
continue;
}
if (Array.isArray(output) &&
output[Common.symbols.errors]) {
mainstay.tracer.log(external.schema, external.state, 'rule', 'external', 'error');
errors.push(...output);
if (settings.abortEarly) {
break;
}
continue;
}
if (parent) {
mainstay.tracer.value(external.state, 'rule', node, output, 'external');
parent[key] = output;
}
else {
mainstay.tracer.value(external.state, 'rule', root, output, 'external');
root = output;
}
}
catch (err) {
if (settings.errors.label) {
err.message += ` (${(external.label)})`; // Change message to include path
}
throw err;
}
}
result.value = root;
if (errors.length) {
result.error = Errors.process(errors, value, settings);
if (mainstay.debug) {
result.error.debug = mainstay.debug;
}
throw result.error;
}
}
if (!settings.warnings &&
!settings.debug &&
!settings.artifacts) {
return result.value;
}
const outcome = { value: result.value };
if (mainstay.warnings.length) {
outcome.warning = Errors.details(mainstay.warnings);
}
if (mainstay.debug) {
outcome.debug = mainstay.debug;
}
if (mainstay.artifacts) {
outcome.artifacts = mainstay.artifacts;
}
return outcome;
};
internals.Mainstay = class {
constructor(tracer, debug, links) {
this.externals = [];
this.warnings = [];
this.tracer = tracer;
this.debug = debug;
this.links = links;
this.shadow = null;
this.artifacts = null;
this._snapshots = [];
}
snapshot() {
this._snapshots.push({
externals: this.externals.slice(),
warnings: this.warnings.slice()
});
}
restore() {
const snapshot = this._snapshots.pop();
this.externals = snapshot.externals;
this.warnings = snapshot.warnings;
}
};
internals.entry = function (value, schema, prefs) {
// Prepare state
const { tracer, cleanup } = internals.tracer(schema, prefs);
const debug = prefs.debug ? [] : null;
const links = schema._ids._schemaChain ? new Map() : null;
const mainstay = new internals.Mainstay(tracer, debug, links);
const schemas = schema._ids._schemaChain ? [{ schema }] : null;
const state = new State([], [], { mainstay, schemas });
// Validate value
const result = exports.validate(value, schema, state, prefs);
// Process value and errors
if (cleanup) {
schema.$_root.untrace();
}
const error = Errors.process(result.errors, value, prefs);
return { value: result.value, error, mainstay };
};
internals.tracer = function (schema, prefs) {
if (schema.$_root._tracer) {
return { tracer: schema.$_root._tracer._register(schema) };
}
if (prefs.debug) {
Assert(schema.$_root.trace, 'Debug mode not supported');
return { tracer: schema.$_root.trace()._register(schema), cleanup: true };
}
return { tracer: internals.ignore };
};
exports.validate = function (value, schema, state, prefs, overrides = {}) {
if (schema.$_terms.whens) {
schema = schema._generate(value, state, prefs).schema;
}
// Setup state and settings
if (schema._preferences) {
prefs = internals.prefs(schema, prefs);
}
// Cache
if (schema._cache &&
prefs.cache) {
const result = schema._cache.get(value);
state.mainstay.tracer.debug(state, 'validate', 'cached', !!result);
if (result) {
return result;
}
}
// Helpers
const createError = (code, local, localState) => schema.$_createError(code, value, local, localState || state, prefs);
const helpers = {
original: value,
prefs,
schema,
state,
error: createError,
errorsArray: internals.errorsArray,
warn: (code, local, localState) => state.mainstay.warnings.push(createError(code, local, localState)),
message: (messages, local) => schema.$_createError('custom', value, local, state, prefs, { messages })
};
// Prepare
state.mainstay.tracer.entry(schema, state);
const def = schema._definition;
if (def.prepare &&
value !== undefined &&
prefs.convert) {
const prepared = def.prepare(value, helpers);
if (prepared) {
state.mainstay.tracer.value(state, 'prepare', value, prepared.value);
if (prepared.errors) {
return internals.finalize(prepared.value, [].concat(prepared.errors), helpers); // Prepare error always aborts early
}
value = prepared.value;
}
}
// Type coercion
if (def.coerce &&
value !== undefined &&
prefs.convert &&
(!def.coerce.from || def.coerce.from.includes(typeof value))) {
const coerced = def.coerce.method(value, helpers);
if (coerced) {
state.mainstay.tracer.value(state, 'coerced', value, coerced.value);
if (coerced.errors) {
return internals.finalize(coerced.value, [].concat(coerced.errors), helpers); // Coerce error always aborts early
}
value = coerced.value;
}
}
// Empty value
const empty = schema._flags.empty;
if (empty &&
empty.$_match(internals.trim(value, schema), state.nest(empty), Common.defaults)) {
state.mainstay.tracer.value(state, 'empty', value, undefined);
value = undefined;
}
// Presence requirements (required, optional, forbidden)
const presence = overrides.presence || schema._flags.presence || (schema._flags._endedSwitch ? null : prefs.presence);
if (value === undefined) {
if (presence === 'forbidden') {
return internals.finalize(value, null, helpers);
}
if (presence === 'required') {
return internals.finalize(value, [schema.$_createError('any.required', value, null, state, prefs)], helpers);
}
if (presence === 'optional') {
if (schema._flags.default !== Common.symbols.deepDefault) {
return internals.finalize(value, null, helpers);
}
state.mainstay.tracer.value(state, 'default', value, {});
value = {};
}
}
else if (presence === 'forbidden') {
return internals.finalize(value, [schema.$_createError('any.unknown', value, null, state, prefs)], helpers);
}
// Allowed values
const errors = [];
if (schema._valids) {
const match = schema._valids.get(value, state, prefs, schema._flags.insensitive);
if (match) {
if (prefs.convert) {
state.mainstay.tracer.value(state, 'valids', value, match.value);
value = match.value;
}
state.mainstay.tracer.filter(schema, state, 'valid', match);
return internals.finalize(value, null, helpers);
}
if (schema._flags.only) {
const report = schema.$_createError('any.only', value, { valids: schema._valids.values({ display: true }) }, state, prefs);
if (prefs.abortEarly) {
return internals.finalize(value, [report], helpers);
}
errors.push(report);
}
}
// Denied values
if (schema._invalids) {
const match = schema._invalids.get(value, state, prefs, schema._flags.insensitive);
if (match) {
state.mainstay.tracer.filter(schema, state, 'invalid', match);
const report = schema.$_createError('any.invalid', value, { invalids: schema._invalids.values({ display: true }) }, state, prefs);
if (prefs.abortEarly) {
return internals.finalize(value, [report], helpers);
}
errors.push(report);
}
}
// Base type
if (def.validate) {
const base = def.validate(value, helpers);
if (base) {
state.mainstay.tracer.value(state, 'base', value, base.value);
value = base.value;
if (base.errors) {
if (!Array.isArray(base.errors)) {
errors.push(base.errors);
return internals.finalize(value, errors, helpers); // Base error always aborts early
}
if (base.errors.length) {
errors.push(...base.errors);
return internals.finalize(value, errors, helpers); // Base error always aborts early
}
}
}
}
// Validate tests
if (!schema._rules.length) {
return internals.finalize(value, errors, helpers);
}
return internals.rules(value, errors, helpers);
};
internals.rules = function (value, errors, helpers) {
const { schema, state, prefs } = helpers;
for (const rule of schema._rules) {
const definition = schema._definition.rules[rule.method];
// Skip rules that are also applied in coerce step
if (definition.convert &&
prefs.convert) {
state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'full');
continue;
}
// Resolve references
let ret;
let args = rule.args;
if (rule._resolve.length) {
args = Object.assign({}, args); // Shallow copy
for (const key of rule._resolve) {
const resolver = definition.argsByName.get(key);
const resolved = args[key].resolve(value, state, prefs);
const normalized = resolver.normalize ? resolver.normalize(resolved) : resolved;
const invalid = Common.validateArg(normalized, null, resolver);
if (invalid) {
ret = schema.$_createError('any.ref', resolved, { arg: key, ref: args[key], reason: invalid }, state, prefs);
break;
}
args[key] = normalized;
}
}
// Test rule
ret = ret || definition.validate(value, helpers, args, rule); // Use ret if already set to reference error
const result = internals.rule(ret, rule);
if (result.errors) {
state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'error');
if (rule.warn) {
state.mainstay.warnings.push(...result.errors);
continue;
}
if (prefs.abortEarly) {
return internals.finalize(value, result.errors, helpers);
}
errors.push(...result.errors);
}
else {
state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'pass');
state.mainstay.tracer.value(state, 'rule', value, result.value, rule.name);
value = result.value;
}
}
return internals.finalize(value, errors, helpers);
};
internals.rule = function (ret, rule) {
if (ret instanceof Errors.Report) {
internals.error(ret, rule);
return { errors: [ret], value: null };
}
if (Array.isArray(ret) &&
ret[Common.symbols.errors]) {
ret.forEach((report) => internals.error(report, rule));
return { errors: ret, value: null };
}
return { errors: null, value: ret };
};
internals.error = function (report, rule) {
if (rule.message) {
report._setTemplate(rule.message);
}
return report;
};
internals.finalize = function (value, errors, helpers) {
errors = errors || [];
const { schema, state, prefs } = helpers;
// Failover value
if (errors.length) {
const failover = internals.default('failover', undefined, errors, helpers);
if (failover !== undefined) {
state.mainstay.tracer.value(state, 'failover', value, failover);
value = failover;
errors = [];
}
}
// Error override
if (errors.length &&
schema._flags.error) {
if (typeof schema._flags.error === 'function') {
errors = schema._flags.error(errors);
if (!Array.isArray(errors)) {
errors = [errors];
}
for (const error of errors) {
Assert(error instanceof Error || error instanceof Errors.Report, 'error() must return an Error object');
}
}
else {
errors = [schema._flags.error];
}
}
// Default
if (value === undefined) {
const defaulted = internals.default('default', value, errors, helpers);
state.mainstay.tracer.value(state, 'default', value, defaulted);
value = defaulted;
}
// Cast
if (schema._flags.cast &&
value !== undefined) {
const caster = schema._definition.cast[schema._flags.cast];
if (caster.from(value)) {
const casted = caster.to(value, helpers);
state.mainstay.tracer.value(state, 'cast', value, casted, schema._flags.cast);
value = casted;
}
}
// Externals
if (schema.$_terms.externals &&
prefs.externals &&
prefs._externals !== false) { // Disabled for matching
for (const { method } of schema.$_terms.externals) {
state.mainstay.externals.push({ method, schema, state, label: Errors.label(schema._flags, state, prefs) });
}
}
// Result
const result = { value, errors: errors.length ? errors : null };
if (schema._flags.result) {
result.value = schema._flags.result === 'strip' ? undefined : /* raw */ helpers.original;
state.mainstay.tracer.value(state, schema._flags.result, value, result.value);
state.shadow(value, schema._flags.result);
}
// Cache
if (schema._cache &&
prefs.cache !== false &&
!schema._refs.length) {
schema._cache.set(helpers.original, result);
}
// Artifacts
if (value !== undefined &&
!result.errors &&
schema._flags.artifact !== undefined) {
state.mainstay.artifacts = state.mainstay.artifacts || new Map();
if (!state.mainstay.artifacts.has(schema._flags.artifact)) {
state.mainstay.artifacts.set(schema._flags.artifact, []);
}
state.mainstay.artifacts.get(schema._flags.artifact).push(state.path);
}
return result;
};
internals.prefs = function (schema, prefs) {
const isDefaultOptions = prefs === Common.defaults;
if (isDefaultOptions &&
schema._preferences[Common.symbols.prefs]) {
return schema._preferences[Common.symbols.prefs];
}
prefs = Common.preferences(prefs, schema._preferences);
if (isDefaultOptions) {
schema._preferences[Common.symbols.prefs] = prefs;
}
return prefs;
};
internals.default = function (flag, value, errors, helpers) {
const { schema, state, prefs } = helpers;
const source = schema._flags[flag];
if (prefs.noDefaults ||
source === undefined) {
return value;
}
state.mainstay.tracer.log(schema, state, 'rule', flag, 'full');
if (!source) {
return source;
}
if (typeof source === 'function') {
const args = source.length ? [Clone(state.ancestors[0]), helpers] : [];
try {
return source(...args);
}
catch (err) {
errors.push(schema.$_createError(`any.${flag}`, null, { error: err }, state, prefs));
return;
}
}
if (typeof source !== 'object') {
return source;
}
if (source[Common.symbols.literal]) {
return source.literal;
}
if (Common.isResolvable(source)) {
return source.resolve(value, state, prefs);
}
return Clone(source);
};
internals.trim = function (value, schema) {
if (typeof value !== 'string') {
return value;
}
const trim = schema.$_getRule('trim');
if (!trim ||
!trim.args.enabled) {
return value;
}
return value.trim();
};
internals.ignore = {
active: false,
debug: Ignore,
entry: Ignore,
filter: Ignore,
log: Ignore,
resolve: Ignore,
value: Ignore
};
internals.errorsArray = function () {
const errors = [];
errors[Common.symbols.errors] = true;
return errors;
};
|