UNPKG

@trapi/metadata

Version:

Generate REST-API metadata scheme from TypeScript Decorators.

142 lines 4.82 kB
"use strict"; /* * Copyright (c) 2021-2023. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getDeclarationValidators = void 0; const js_doc_1 = require("../js-doc"); const constants_1 = require("./constants"); function getDeclarationValidators(declaration, name) { if (!declaration.parent) { return {}; } const getCommentValue = (comment) => comment && comment.split(' ')[0]; const parameterTags = getSupportedParameterTags(); const tags = (0, js_doc_1.getJSDocTags)(declaration.parent, (tag) => { const { comment } = tag; const text = (0, js_doc_1.transformJSDocComment)(comment); if (!comment) { return false; } const commentValue = getCommentValue(text); return parameterTags.some((value) => { if (value !== tag.tagName.text) { return false; } return !(name && name !== commentValue); }); }); function getErrorMsg(comment, isValue = true) { if (!comment) { return undefined; } if (isValue) { const indexOf = comment.indexOf(' '); if (indexOf > 0) { return comment.substring(indexOf + 1); } return undefined; } return comment; } const validators = {}; for (let i = 0; i < tags.length; i++) { const tag = tags[i]; if (!tag.comment) { continue; } const name = tag.tagName.text; let comment = (0, js_doc_1.transformJSDocComment)(tag.comment); comment = comment.substring(comment.indexOf(' ') + 1).trim(); const value = getCommentValue(comment); switch (name) { case constants_1.ValidatorName.UNIQUE_ITEMS: validators[name] = { message: getErrorMsg(comment, false), value: undefined, }; break; case constants_1.ValidatorName.MINIMUM: case constants_1.ValidatorName.MAXIMUM: case constants_1.ValidatorName.MIN_ITEMS: case constants_1.ValidatorName.MAX_ITEMS: case constants_1.ValidatorName.MIN_LENGTH: case constants_1.ValidatorName.MAX_LENGTH: if (Number.isNaN(value)) { throw new Error(`${name} parameter use number.`); } validators[name] = { message: getErrorMsg(comment), value: Number(value), }; break; case constants_1.ValidatorName.MIN_DATE: case constants_1.ValidatorName.MAX_DATE: if (typeof value !== 'string') { throw new Error(`${name} parameter use date format ISO 8601 ex. 2017-05-14, 2017-05-14T05:18Z`); } validators[name] = { message: getErrorMsg(comment), value, }; break; case constants_1.ValidatorName.PATTERN: if (typeof value !== 'string') { throw new Error(`${name} parameter use string.`); } validators[name] = { message: getErrorMsg(comment), value: removeSurroundingQuotes(value), }; break; default: if (name.toLowerCase().startsWith('is')) { const errorMsg = getErrorMsg(comment, false); if (errorMsg) { validators[name] = { message: errorMsg, value: undefined, }; } } break; } } return validators; } exports.getDeclarationValidators = getDeclarationValidators; function getSupportedParameterTags() { return [ 'isString', 'isBoolean', 'isInt', 'isLong', 'isFloat', 'isDouble', 'isDate', 'isDateTime', 'minItems', 'maxItems', 'uniqueItems', 'minLength', 'maxLength', 'pattern', 'minimum', 'maximum', 'minDate', 'maxDate', ]; } function removeSurroundingQuotes(str) { if (str.startsWith('`') && str.endsWith('`')) { return str.substring(1, str.length - 1); } if (str.startsWith('```') && str.endsWith('```')) { return str.substring(3, str.length - 3); } return str; } //# sourceMappingURL=module.js.map