UNPKG

stringiful

Version:

Easy to use stringify function with built-in configurable formatters

89 lines 4.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringifyConfigSchema = void 0; const joi_1 = __importDefault(require("joi")); const interface_1 = require("./formatters/interface"); const timezones_1 = require("./formatters/basicTypes/date/timezones"); const config_1 = __importDefault(require("./config")); const { stringifyDefaultOptions: { inspectOptions: { breakLength, depth }, }, } = config_1.default; const stringFormatterParamsSchema = joi_1.default.object({ maxLength: joi_1.default.number(), }); const axiosErrorFormatterParamsSchema = joi_1.default.object({ maxResponseDataLength: joi_1.default.number(), maxRequestDataLength: joi_1.default.number(), }); const errorFormatterParamsSchema = joi_1.default.object({ maxMessageLength: joi_1.default.number(), }); const dateFormatterParamsSchema = joi_1.default.object({ timezone: joi_1.default.string().valid(...timezones_1.timeZoneOptions), locale: joi_1.default.string(), }); const doesFormatterConfigsAreForTheSameMatch = (first, second) => { return first.matches === second.matches; }; const getFormatterParamsValidation = (formatterName) => { switch (formatterName) { case 'axiosError': return joi_1.default.when('matches', { is: 'axiosError', then: axiosErrorFormatterParamsSchema }); case 'date': return joi_1.default.when('matches', { is: 'date', then: dateFormatterParamsSchema }); case 'string': return joi_1.default.when('matches', { is: 'string', then: stringFormatterParamsSchema }); case 'error': return joi_1.default.when('matches', { is: 'error', then: errorFormatterParamsSchema }); default: throw new Error('BUG - got unknown formatter type'); } }; const getParamsValidation = () => { const formattersSchemas = interface_1.FormatterTypeOptions.map((formatterType) => { return getFormatterParamsValidation(formatterType); }); return formattersSchemas.reduce((accumulator, currentValue) => accumulator.concat(currentValue)); }; const supportBlacklistWhitelist = ['axiosError', 'error']; const formatterConfigSchema = joi_1.default.object({ matches: joi_1.default.function() .arity(1) .allow(...interface_1.FormatterTypeOptions) .required() .error(new Error(`Matches should be a function or one of the following strings: ${interface_1.FormatterTypeOptions}`)), format: joi_1.default.function().arity(1), fieldsBlacklist: joi_1.default.when('matches', { is: joi_1.default.valid(...supportBlacklistWhitelist), then: joi_1.default.array().items(joi_1.default.string()), otherwise: joi_1.default.forbidden(), }), fieldsWhitelist: joi_1.default.when('matches', { is: joi_1.default.valid(...supportBlacklistWhitelist), then: joi_1.default.array().items(joi_1.default.string()), otherwise: joi_1.default.forbidden(), }), params: getParamsValidation(), }) .nand('fieldsWhitelist', 'fieldsBlacklist') .nand('format', 'params') .or('format', 'fieldsWhitelist', 'fieldsBlacklist', 'params'); const inspectOptionsSchema = joi_1.default.object({ getters: joi_1.default.valid('get', 'set', true, false), showHidden: joi_1.default.boolean(), depth: joi_1.default.number().allow(null, Infinity).default(depth), colors: joi_1.default.boolean(), customInspect: joi_1.default.boolean(), showProxy: joi_1.default.boolean(), maxArrayLength: joi_1.default.number().allow(null), maxStringLength: joi_1.default.number().allow(null), breakLength: joi_1.default.number().default(breakLength), compact: joi_1.default.number().allow(true, false), sorted: joi_1.default.boolean().allow(joi_1.default.function()), }); exports.stringifyConfigSchema = joi_1.default.object({ formatters: joi_1.default.array().items(formatterConfigSchema).unique(doesFormatterConfigsAreForTheSameMatch), inspectOptions: inspectOptionsSchema, }); //# sourceMappingURL=validation.schema.js.map