|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const get = require("lodash.get"); |
| 4 | +function niceJoin(array, lastSeparator = ' and ', separator = ', ') { |
| 5 | + switch (array.length) { |
| 6 | + case 0: |
| 7 | + return ''; |
| 8 | + case 1: |
| 9 | + return array[0]; |
| 10 | + case 2: |
| 11 | + return array.join(lastSeparator); |
| 12 | + default: |
| 13 | + return array.slice(0, array.length - 1).join(separator) + lastSeparator + array[array.length - 1]; |
| 14 | + } |
| 15 | +} |
| 16 | +exports.niceJoin = niceJoin; |
| 17 | +exports.validationMessagesFormatters = { |
| 18 | + minimum: (min) => `must be a number greater than or equal to ${min}`, |
| 19 | + maximum: (max) => `must be a number less than or equal to ${max}`, |
| 20 | + minimumProperties(min) { |
| 21 | + return min === 1 ? 'cannot be a empty object' : `must be a object with at least ${min} properties`; |
| 22 | + }, |
| 23 | + maximumProperties(max) { |
| 24 | + return max === 0 ? 'must be a empty object' : `must be a object with at most ${max} properties`; |
| 25 | + }, |
| 26 | + minimumItems(min) { |
| 27 | + return min === 1 ? 'cannot be a empty array' : `must be an array with at least ${min} items`; |
| 28 | + }, |
| 29 | + maximumItems(max) { |
| 30 | + return max === 0 ? 'must be a empty array' : `must be an array with at most ${max} items`; |
| 31 | + }, |
| 32 | + enum: (values) => `must be one of the following values: ${niceJoin(values.map((f) => `"${f}"`), ' or ')}`, |
| 33 | + pattern: (pattern) => `must match pattern "${pattern.replace(/\(\?\:/g, '(')}"`, |
| 34 | + invalidResponseCode: (code) => `This endpoint cannot respond with HTTP status ${code}.`, |
| 35 | + invalidResponse: (code) => `The response returned from the endpoint violates its specification for the HTTP status ${code}.` |
| 36 | +}; |
| 37 | +exports.validationMessages = { |
| 38 | + contentType: 'only JSON payloads are accepted. Please set the "Content-Type" header to start with "application/json"', |
| 39 | + json: 'the body payload is not a valid JSON', |
| 40 | + jsonEmpty: 'the JSON body payload cannot be empty if the "Content-Type" header is set', |
| 41 | + missing: 'must be present', |
| 42 | + unknown: 'is not a valid property', |
| 43 | + emptyObject: 'cannot be a empty object', |
| 44 | + uuid: 'must be a valid GUID (UUID v4)', |
| 45 | + timestamp: 'must be a valid ISO 8601 / RFC 3339 timestamp (example: 2018-07-06T12:34:56Z)', |
| 46 | + date: 'must be a valid ISO 8601 / RFC 3339 date (example: 2018-07-06)', |
| 47 | + time: 'must be a valid ISO 8601 / RFC 3339 time (example: 12:34:56)', |
| 48 | + hostname: 'must be a valid hostname', |
| 49 | + ip: 'must be a valid IPv4 or IPv6', |
| 50 | + ipv4: 'must be a valid IPv4', |
| 51 | + ipv6: 'must be a valid IPv6', |
| 52 | + integer: 'must be a valid integer number', |
| 53 | + number: 'must be a valid number', |
| 54 | + boolean: 'must be a valid boolean (true or false)', |
| 55 | + object: 'must be a object', |
| 56 | + array: 'must be an array', |
| 57 | + string: 'must be a string', |
| 58 | + presentString: 'must be a non empty string' |
| 59 | +}; |
| 60 | +function convertValidationErrors(section, data, validationErrors) { |
| 61 | + const errors = {}; |
| 62 | + if (section === 'querystring') { |
| 63 | + section = 'query'; |
| 64 | + } |
| 65 | + for (const e of validationErrors) { |
| 66 | + // For each error |
| 67 | + let baseKey = e.dataPath.substring(e.dataPath.startsWith('.') ? 1 : 0); |
| 68 | + let key = baseKey; |
| 69 | + let message = ''; |
| 70 | + // Depending on the type |
| 71 | + switch (e.keyword) { |
| 72 | + case 'required': |
| 73 | + case 'dependencies': |
| 74 | + key = e.params.missingProperty; |
| 75 | + message = exports.validationMessages.missing; |
| 76 | + break; |
| 77 | + case 'additionalProperties': |
| 78 | + key = e.params.additionalProperty; |
| 79 | + message = exports.validationMessages.unknown; |
| 80 | + break; |
| 81 | + case 'type': |
| 82 | + message = exports.validationMessages[e.params.type]; |
| 83 | + break; |
| 84 | + case 'minProperties': |
| 85 | + message = exports.validationMessagesFormatters.minimumProperties(e.params.limit); |
| 86 | + break; |
| 87 | + case 'maxProperties': |
| 88 | + message = exports.validationMessagesFormatters.maximumProperties(e.params.limit); |
| 89 | + break; |
| 90 | + case 'minItems': |
| 91 | + message = exports.validationMessagesFormatters.minimumItems(e.params.limit); |
| 92 | + break; |
| 93 | + case 'maxItems': |
| 94 | + message = exports.validationMessagesFormatters.maximumItems(e.params.limit); |
| 95 | + break; |
| 96 | + case 'minimum': |
| 97 | + message = exports.validationMessagesFormatters.minimum(e.params.limit); |
| 98 | + break; |
| 99 | + case 'maximum': |
| 100 | + message = exports.validationMessagesFormatters.maximum(e.params.limit); |
| 101 | + break; |
| 102 | + case 'enum': |
| 103 | + message = exports.validationMessagesFormatters.enum(e.params.allowedValues); |
| 104 | + break; |
| 105 | + case 'pattern': |
| 106 | + const pattern = e.params.pattern; |
| 107 | + const value = get(data, key); |
| 108 | + if (pattern === '.+' && !value) { |
| 109 | + message = exports.validationMessages.presentString; |
| 110 | + } |
| 111 | + else { |
| 112 | + message = exports.validationMessagesFormatters.pattern(e.params.pattern); |
| 113 | + } |
| 114 | + break; |
| 115 | + case 'format': |
| 116 | + let reason = e.params.format; |
| 117 | + // Normalize the key |
| 118 | + if (reason === 'date-time') { |
| 119 | + reason = 'timestamp'; |
| 120 | + } |
| 121 | + message = exports.validationMessagesFormatters[reason] |
| 122 | + ? exports.validationMessagesFormatters[reason](reason) |
| 123 | + : exports.validationMessages[reason]; |
| 124 | + break; |
| 125 | + } |
| 126 | + // No custom message was found, default to input one replacing the starting verb and adding some path info |
| 127 | + if (!message) { |
| 128 | + message = `${e.message.replace(/^should/, 'must')} (${e.keyword})`; |
| 129 | + } |
| 130 | + // Find the property to add |
| 131 | + let property = Array.from(new Set([baseKey, key].filter((p) => p))) |
| 132 | + .join('.') |
| 133 | + .replace(/\[(\d+)\]/g, '.$1'); |
| 134 | + if (!property) { |
| 135 | + property = '$root'; |
| 136 | + } |
| 137 | + errors[property] = message; |
| 138 | + } |
| 139 | + return { [section]: errors }; |
| 140 | +} |
| 141 | +exports.convertValidationErrors = convertValidationErrors; |
0 commit comments