array.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const schema_1 = require("../schema");
  5. class ArraySchema extends schema_1.Schema {
  6. constructor(_a) {
  7. var { valueSchema, name = valueSchema.name } = _a, handlers = tslib_1.__rest(_a, ["valueSchema", "name"]);
  8. super(Object.assign({}, handlers, { name }));
  9. this._valueSchema = valueSchema;
  10. }
  11. expected(utils) {
  12. return `an array of ${this._valueSchema.expected(utils)}`;
  13. }
  14. validate(value, utils) {
  15. if (!Array.isArray(value)) {
  16. return false;
  17. }
  18. const invalidValues = [];
  19. for (const subValue of value) {
  20. const subValidateResult = utils.normalizeValidateResult(this._valueSchema.validate(subValue, utils), subValue);
  21. if (subValidateResult !== true) {
  22. invalidValues.push(subValidateResult.value);
  23. }
  24. }
  25. return invalidValues.length === 0 ? true : { value: invalidValues };
  26. }
  27. deprecated(value, utils) {
  28. const deprecatedResult = [];
  29. for (const subValue of value) {
  30. const subDeprecatedResult = utils.normalizeDeprecatedResult(this._valueSchema.deprecated(subValue, utils), subValue);
  31. if (subDeprecatedResult !== false) {
  32. deprecatedResult.push(...subDeprecatedResult.map(({ value: deprecatedValue }) => ({
  33. value: [deprecatedValue],
  34. })));
  35. }
  36. }
  37. return deprecatedResult;
  38. }
  39. forward(value, utils) {
  40. const forwardResult = [];
  41. for (const subValue of value) {
  42. const subForwardResult = utils.normalizeForwardResult(this._valueSchema.forward(subValue, utils), subValue);
  43. forwardResult.push(...subForwardResult.map(wrapTransferResult));
  44. }
  45. return forwardResult;
  46. }
  47. redirect(value, utils) {
  48. const remain = [];
  49. const redirect = [];
  50. for (const subValue of value) {
  51. const subRedirectResult = utils.normalizeRedirectResult(this._valueSchema.redirect(subValue, utils), subValue);
  52. if ('remain' in subRedirectResult) {
  53. remain.push(subRedirectResult.remain);
  54. }
  55. redirect.push(...subRedirectResult.redirect.map(wrapTransferResult));
  56. }
  57. return remain.length === 0 ? { redirect } : { redirect, remain };
  58. }
  59. overlap(currentValue, newValue) {
  60. return currentValue.concat(newValue);
  61. }
  62. }
  63. exports.ArraySchema = ArraySchema;
  64. function wrapTransferResult({ from, to }) {
  65. return { from: [from], to };
  66. }