File size: 7,362 Bytes
945aae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var leven = _interopDefault(require('leven'));
var pointer = _interopDefault(require('jsonpointer'));

function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

var AJV_ERROR_KEYWORD_WEIGHT_MAP = {
  "enum": 1,
  type: 0
};
var QUOTES_REGEX = /"/g;
var NOT_REGEX = /NOT/g;
var SLASH_REGEX = /\//g;

var filterSingleErrorPerProperty = function filterSingleErrorPerProperty(errors) {
  var errorsPerProperty = errors.reduce(function (acc, error) {
    var _ref, _error$params$additio, _error$params, _error$params2, _AJV_ERROR_KEYWORD_WE, _AJV_ERROR_KEYWORD_WE2;

    var prop = error.instancePath + ((_ref = (_error$params$additio = (_error$params = error.params) == null ? void 0 : _error$params.additionalProperty) != null ? _error$params$additio : (_error$params2 = error.params) == null ? void 0 : _error$params2.missingProperty) != null ? _ref : '');
    var existingError = acc[prop];

    if (!existingError) {
      acc[prop] = error;
      return acc;
    }

    var weight = (_AJV_ERROR_KEYWORD_WE = AJV_ERROR_KEYWORD_WEIGHT_MAP[error.keyword]) != null ? _AJV_ERROR_KEYWORD_WE : 0;
    var existingWeight = (_AJV_ERROR_KEYWORD_WE2 = AJV_ERROR_KEYWORD_WEIGHT_MAP[existingError.keyword]) != null ? _AJV_ERROR_KEYWORD_WE2 : 0;

    if (weight > existingWeight) {
      acc[prop] = error;
    }

    return acc;
  }, {});
  return Object.values(errorsPerProperty);
};

var getSuggestion = function getSuggestion(_ref) {
  var value = _ref.value,
      suggestions = _ref.suggestions,
      _ref$format = _ref.format,
      format = _ref$format === void 0 ? function (suggestion) {
    return "Did you mean '" + suggestion + "'?";
  } : _ref$format;
  if (!value) return '';
  var bestSuggestion = suggestions.reduce(function (best, current) {
    var distance = leven(value, current);

    if (best.distance > distance) {
      return {
        value: current,
        distance: distance
      };
    }

    return best;
  }, {
    distance: Infinity,
    value: ''
  });
  return bestSuggestion.distance < value.length ? format(bestSuggestion.value) : '';
};

var pointerToDotNotation = function pointerToDotNotation(pointer) {
  return pointer.replace(SLASH_REGEX, '.');
};
var cleanAjvMessage = function cleanAjvMessage(message) {
  return message.replace(QUOTES_REGEX, "'").replace(NOT_REGEX, 'not');
};
var getLastSegment = function getLastSegment(path) {
  var segments = path.split('/');
  return segments.pop();
};
var safeJsonPointer = function safeJsonPointer(_ref) {
  var object = _ref.object,
      pnter = _ref.pnter,
      fallback = _ref.fallback;

  try {
    return pointer.get(object, pnter);
  } catch (err) {
    return fallback;
  }
};

var betterAjvErrors = function betterAjvErrors(_ref) {
  var errors = _ref.errors,
      data = _ref.data,
      schema = _ref.schema,
      _ref$basePath = _ref.basePath,
      basePath = _ref$basePath === void 0 ? '{base}' : _ref$basePath;

  if (!Array.isArray(errors) || errors.length === 0) {
    return [];
  }

  var definedErrors = filterSingleErrorPerProperty(errors);
  return definedErrors.map(function (error) {
    var path = pointerToDotNotation(basePath + error.instancePath);
    var prop = getLastSegment(error.instancePath);
    var defaultContext = {
      errorType: error.keyword
    };
    var defaultMessage = (prop ? "property '" + prop + "'" : path) + " " + cleanAjvMessage(error.message);
    var validationError;

    switch (error.keyword) {
      case 'additionalProperties':
        {
          var additionalProp = error.params.additionalProperty;
          var suggestionPointer = error.schemaPath.replace('#', '').replace('/additionalProperties', '');

          var _safeJsonPointer = safeJsonPointer({
            object: schema,
            pnter: suggestionPointer,
            fallback: {
              properties: {}
            }
          }),
              properties = _safeJsonPointer.properties;

          validationError = {
            message: "'" + additionalProp + "' property is not expected to be here",
            suggestion: getSuggestion({
              value: additionalProp,
              suggestions: Object.keys(properties != null ? properties : {}),
              format: function format(suggestion) {
                return "Did you mean property '" + suggestion + "'?";
              }
            }),
            path: path,
            context: defaultContext
          };
          break;
        }

      case 'enum':
        {
          var suggestions = error.params.allowedValues.map(function (value) {
            return String(value != null ? value : '');
          });

          var _prop = getLastSegment(error.instancePath);

          var value = safeJsonPointer({
            object: data,
            pnter: error.instancePath,
            fallback: ''
          });
          validationError = {
            message: "'" + _prop + "' property must be equal to one of the allowed values",
            suggestion: getSuggestion({
              value: value,
              suggestions: suggestions
            }),
            path: path,
            context: _extends({}, defaultContext, {
              allowedValues: error.params.allowedValues
            })
          };
          break;
        }

      case 'type':
        {
          var _prop2 = getLastSegment(error.instancePath);

          var type = error.params.type;
          validationError = {
            message: "'" + _prop2 + "' property type must be " + type,
            path: path,
            context: defaultContext
          };
          break;
        }

      case 'required':
        {
          validationError = {
            message: path + " must have required property '" + error.params.missingProperty + "'",
            path: path,
            context: defaultContext
          };
          break;
        }

      case 'const':
        {
          return {
            message: "'" + prop + "' property must be equal to the allowed value",
            path: path,
            context: _extends({}, defaultContext, {
              allowedValue: error.params.allowedValue
            })
          };
        }

      default:
        return {
          message: defaultMessage,
          path: path,
          context: defaultContext
        };
    } // Remove empty properties


    var errorEntries = Object.entries(validationError);

    for (var _i = 0, _errorEntries = errorEntries; _i < _errorEntries.length; _i++) {
      var _errorEntries$_i = _errorEntries[_i],
          key = _errorEntries$_i[0],
          _value = _errorEntries$_i[1];

      if (_value === null || _value === undefined || _value === '') {
        delete validationError[key];
      }
    }

    return validationError;
  });
};

exports.betterAjvErrors = betterAjvErrors;
//# sourceMappingURL=better-ajv-errors.cjs.development.js.map