UNPKG

rtech-struct

Version:

Rtech Platform Superstruct Entity

32 lines (27 loc) 1.61 kB
const { type, optional, array, enums, number, refine } = require('superstruct') const { Currency } = require('./currency') const { PriceByCurrencyItem } = require('./prices_by_currency') /** * Creates a superstruct type for a partial bid object with optional properties. * The tags property is an array of strings, and its values are validated against the authorizedTags array. * If maxTags is defined, the length of the tags array is also validated to ensure it doesn't exceed maxTags. * * @param {Object} config - Configuration object for the struct. * @param {string[]} [config.authorizedTags] - Array of authorized tag strings. Defaults to an empty array if not provided. * @param {number | undefined} [config.maxTags] - Maximum number of tags allowed. If defined, the length of the tags array should not exceed this number. If undefined, then there's no limit of the quantity of tags. * @returns {superstruct.Struct} A superstruct type representing the struct. */ exports.PartialBid = (config = {}) => { const authorizedTags = Array.isArray(config?.authorizedTags) ? config.authorizedTags : [] const tagsStruct = array(enums(authorizedTags)) // If maxTags is defined, refine the tags structure to ensure its length doesn't exceed maxTags const refinedTagsStruct = config?.maxTags && typeof config?.maxTags === 'number' ? refine(tagsStruct, 'Max tags exceeded', (tags) => tags.length <= config.maxTags) : tagsStruct return type({ tags: optional(refinedTagsStruct), maxTags: optional(number()), prices_by_currency: optional(PriceByCurrencyItem) }) }