UNPKG

@adguard/agtree

Version:
80 lines (78 loc) 2.69 kB
/* * AGTree v3.2.2 (build date: Tue, 08 Jul 2025 13:39:47 GMT) * (c) 2025 Adguard Software Ltd. * Released under the MIT license * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme */ import zod from 'zod'; import { zodToCamelCase } from '../utils/zod-camelcase.js'; import { booleanSchema, nonEmptyStringSchema, baseCompatibilityDataSchema, baseRefineLogic } from './base.js'; /** * @file Schema for scriptlet data. */ /** * Zod schema for scriptlet parameter data. */ const scriptletParameterSchema = zod.object({ /** * Name of the actual parameter. */ name: nonEmptyStringSchema, /** * Describes whether the parameter is required. Empty parameters are not allowed. */ required: booleanSchema, /** * Short description of the parameter. * If not specified or it's value is `null`,then the description is not available. */ description: nonEmptyStringSchema.nullable().default(null), /** * Regular expression that matches the value of the parameter. * If it's value is `null`, then the parameter value is not checked. */ pattern: nonEmptyStringSchema.nullable().default(null), /** * Default value of the parameter (if any). */ default: nonEmptyStringSchema.nullable().default(null), /** * Describes whether the parameter is used only for debugging purposes. */ debug: booleanSchema.default(false), }); /** * Zod schema for scriptlet parameters. */ const scriptletParametersSchema = zod.array(scriptletParameterSchema); /** * Zod schema for scriptlet data. */ zodToCamelCase(baseCompatibilityDataSchema.extend({ /** * List of parameters that the scriptlet accepts. * **Every** parameter should be listed here, because we check that the scriptlet is used correctly * (e.g. that the number of parameters is correct). */ parameters: scriptletParametersSchema.optional(), }).superRefine((data, ctx) => { // TODO: find something better, for now we can't add refine logic to the base schema: // https://github.com/colinhacks/zod/issues/454#issuecomment-848370721 baseRefineLogic(data, ctx); // we don't allow required parameters after optional ones if (!data.parameters) { return; } let optionalFound = false; for (const parameter of data.parameters) { if (optionalFound && parameter.required) { ctx.addIssue({ code: zod.ZodIssueCode.custom, message: 'Required parameters must be before optional ones', }); } if (!parameter.required) { optionalFound = true; } } }));