UNPKG

@featurevisor/core

Version:

Core package of Featurevisor for Node.js usage

168 lines 6.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConditionsZodSchema = getConditionsZodSchema; const zod_1 = require("zod"); const commonOperators = ["equals", "notEquals"]; const numericOperators = ["greaterThan", "greaterThanOrEquals", "lessThan", "lessThanOrEquals"]; const stringOperators = [ "contains", "notContains", "startsWith", "endsWith", "includes", "notIncludes", ]; const semverOperators = [ "semverEquals", "semverNotEquals", "semverGreaterThan", "semverGreaterThanOrEquals", "semverLessThan", "semverLessThanOrEquals", ]; const dateOperators = ["before", "after"]; const arrayOperators = ["in", "notIn"]; const regexOperators = ["matches", "notMatches"]; const operatorsWithoutValue = ["exists", "notExists"]; function getConditionsZodSchema(projectConfig, availableAttributeKeys) { const plainConditionZodSchema = zod_1.z .object({ attribute: zod_1.z.string().refine((value) => availableAttributeKeys.includes(value), (value) => ({ message: `Unknown attribute "${value}"`, })), operator: zod_1.z.enum([ ...commonOperators, ...numericOperators, ...stringOperators, ...semverOperators, ...dateOperators, ...arrayOperators, ...regexOperators, ...operatorsWithoutValue, ]), value: zod_1.z .union([zod_1.z.string(), zod_1.z.array(zod_1.z.string()), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.date(), zod_1.z.null()]) .optional(), regexFlags: zod_1.z .string() .refine((value) => { if (typeof value === "undefined") { return true; } return /^[gimsuy]{1,}$/.test(value); }, { message: `regexFlags must of one or more of these characters: g, i, m, s, u, y`, }) .optional(), }) .superRefine((data, context) => { // common if (commonOperators.includes(data.operator) && !(data.value === null || typeof data.value === "string" || typeof data.value === "number" || typeof data.value === "boolean" || data.value instanceof Date || data.value === null)) { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value has to be either a string, number, boolean, date or null`, path: ["value"], }); } // numeric if (numericOperators.includes(data.operator) && typeof data.value !== "number") { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be a number`, path: ["value"], }); } // string if (stringOperators.includes(data.operator) && typeof data.value !== "string") { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be a string`, path: ["value"], }); } // semver if (semverOperators.includes(data.operator) && typeof data.value !== "string") { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be a string`, path: ["value"], }); } // date if (dateOperators.includes(data.operator) && !(data.value instanceof Date)) { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be a date`, path: ["value"], }); } // array if (arrayOperators.includes(data.operator) && !Array.isArray(data.value)) { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be an array of strings`, path: ["value"], }); } // regex if (regexOperators.includes(data.operator)) { if (typeof data.value !== "string") { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value must be a string`, path: ["value"], }); } } else { // regex flags are not needed if (data.regexFlags) { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is nether "matches" nor "notMatches", regexFlags are not needed`, path: ["regexFlags"], }); } } // operators without value if (operatorsWithoutValue.includes(data.operator) && data.value !== undefined) { context.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: `when operator is "${data.operator}", value is not needed`, path: ["value"], }); } }); const andOrNotConditionZodSchema = zod_1.z.union([ zod_1.z .object({ and: zod_1.z.array(zod_1.z.lazy(() => conditionZodSchema)), }) .strict(), zod_1.z .object({ or: zod_1.z.array(zod_1.z.lazy(() => conditionZodSchema)), }) .strict(), zod_1.z .object({ not: zod_1.z.array(zod_1.z.lazy(() => conditionZodSchema)), }) .strict(), ]); const everyoneZodSchema = zod_1.z.literal("*"); const conditionZodSchema = zod_1.z.union([andOrNotConditionZodSchema, plainConditionZodSchema]); const conditionsZodSchema = zod_1.z.union([ conditionZodSchema, zod_1.z.array(conditionZodSchema), everyoneZodSchema, ]); return conditionsZodSchema; } //# sourceMappingURL=conditionSchema.js.map