nestjs-typebox
Version:
This library provides helper utilities for writing and validating NestJS APIs using [TypeBox](https://github.com/sinclairzx81/typebox) as an alternative to class-validator/class-transformer. Can be configured to patch @nestjs/swagger allowing OpenAPI gene
65 lines • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setFormats = exports.dateTimeFormat = exports.timeFormat = exports.dateFormat = exports.urlFormat = exports.uuidFormat = exports.emailFormat = void 0;
const typebox_1 = require("@sinclair/typebox");
const emailRegex = /.+\@.+\..+/;
const emailFormat = (value) => emailRegex.test(value);
exports.emailFormat = emailFormat;
const uuidRegex = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
const uuidFormat = (value) => uuidRegex.test(value);
exports.uuidFormat = uuidFormat;
const urlRegex = /^(?:https?|wss?|ftp):\/\/(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)(?:\.(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu;
const urlFormat = (value) => urlRegex.test(value);
exports.urlFormat = urlFormat;
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
const isLeapYear = (year) => {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
};
const dateFormat = (value) => {
const matches = DATE.exec(value);
if (!matches)
return false;
const year = +matches[1];
const month = +matches[2];
const day = +matches[3];
return month >= 1 && month <= 12 && day >= 1 && day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month]);
};
exports.dateFormat = dateFormat;
const timeRegex = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i;
const timeFormat = (value, strictTimeZone) => {
const matches = timeRegex.exec(value);
if (!matches)
return false;
const hr = +matches[1];
const min = +matches[2];
const sec = +matches[3];
const tz = matches[4];
const tzSign = matches[5] === '-' ? -1 : 1;
const tzH = +(matches[6] || 0);
const tzM = +(matches[7] || 0);
if (tzH > 23 || tzM > 59 || (strictTimeZone && !tz))
return false;
if (hr <= 23 && min <= 59 && sec < 60)
return true;
const utcMin = min - tzM * tzSign;
const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0);
return (utcHr === 23 || utcHr === -1) && (utcMin === 59 || utcMin === -1) && sec < 61;
};
exports.timeFormat = timeFormat;
const dateTimeSplitRegex = /t|\s/i;
const dateTimeFormat = (value, strictTimeZone) => {
const dateTime = value.split(dateTimeSplitRegex);
return dateTime.length === 2 && (0, exports.dateFormat)(dateTime[0]) && (0, exports.timeFormat)(dateTime[1], strictTimeZone);
};
exports.dateTimeFormat = dateTimeFormat;
const setFormats = () => {
typebox_1.FormatRegistry.Set('email', exports.emailFormat);
typebox_1.FormatRegistry.Set('uuid', exports.uuidFormat);
typebox_1.FormatRegistry.Set('url', exports.urlFormat);
typebox_1.FormatRegistry.Set('date', exports.dateFormat);
typebox_1.FormatRegistry.Set('time', exports.timeFormat);
typebox_1.FormatRegistry.Set('date-time', exports.dateTimeFormat);
};
exports.setFormats = setFormats;
//# sourceMappingURL=formats.js.map