UNPKG

@sauce-api/core

Version:

Sauce API core functionality

69 lines (68 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouteParam = void 0; /** * Defines a route param for Sauce to pickup. * Could be a param in the path, body, or a query param * * @example * new RouteParam() .setName("startDate") .setType("string") .setDescription("ISO string of the start date of this thing") .setRequired(true) .setCustomValidator(someCustomValidator) */ class RouteParam { setName(name) { this.name = name; return this; } setChildren(children) { this.children = children; return this; } setRequired(required) { this.required = required; return this; } setType(type) { this.type = type; return this; } setEnumValues(values) { this.enumValues = values; return this; } setDescription(description) { this.description = description; return this; } /** * Set a custom validator on this route param * @param {Function} validator * @return {RouteParam} * @example * const isIsoDateParam = (requestParamValue :any, req :Request)=>{ // This will enforce that the incoming string for the route param is indeed foramtted as an ISO string const errorRes = formatError(400, "Date string must be in ISO8601:2000 format"); if (!/d{4}-d{2}-d{2}Td{2}:d{2}:d{2}.d{3}Z/.test(requestParamValue)) { throw errorRes; }; const d = new Date(requestParamValue); const isValid = Date && !isNaN(d as any) && d.toISOString()===requestParamValue; // valid date if(!isValid) { throw errorRes; } }; * * new RouteParam() * .setCustomValidator(isIsoDateParam) */ setCustomValidator(validator) { this.customValidator = validator; return this; } } exports.RouteParam = RouteParam;