Skip to content

Commit 659b17c

Browse files
committed
modify the logic for generating schema for single parameters
1 parent 7b646a0 commit 659b17c

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/core/friendly_errors/param_validator.js

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,52 +132,47 @@ function generateZodSchemasForFunc(func) {
132132
overloads = funcInfo.overloads;
133133
}
134134

135-
// Generate a schema for a single parameter that can be of multiple constants.
136-
// Note that because we're ultimately interested in the value of the constant,
137-
// mapping constants to their values via `constantsMap` is necessary.
138-
//
139-
// Also, z.num() can only be used for a fixed set of allowable string values.
140-
// However, our constants sometimes have numeric or non-primitive values.
141-
// Therefore, z.union() is used here instead.
142-
const generateConstantSchema = constants => {
143-
return z.union(constants.map(c => z.literal(constantsMap[c])));
144-
}
145-
146-
// Generate a schema for a single parameter that can be of multiple
147-
// non-constant types, i.e. `String|Number|Array`.
148-
const generateUnionSchema = types => {
149-
return z.union(types
150-
.filter(t => {
151-
if (!schemaMap[t]) {
152-
console.log(`Warning: Zod schema not found for type '${t}'. Skip mapping`);
153-
return false;
154-
}
155-
return true;
156-
})
157-
.map(t => schemaMap[t]));
158-
}
135+
// Returns a schema for a single type, i.e. z.boolean() for `boolean`.
136+
const generateTypeSchema = type => {
137+
// Type only contains uppercase letters and underscores -> type is a
138+
// constant. Note that because we're ultimately interested in the value of
139+
// the constant, mapping constants to their values via `constantsMap` is
140+
// necessary.
141+
if (/^[A-Z_]+$/.test(type)) {
142+
return z.literal(constantsMap[type]);
143+
} else if (schemaMap[type]) {
144+
return schemaMap[type];
145+
} else {
146+
// TODO: Make this throw an error once more types are supported.
147+
console.log(`Warning: Zod schema not found for type '${type}'. Skip mapping`);
148+
return undefined;
149+
}
150+
};
159151

160-
// Generate a schema for a single parameter.
152+
// Generate a schema for a single parameter. In the case where a parameter can
153+
// be of multiple types, `generateTypeSchema` is called for each type.
161154
const generateParamSchema = param => {
162155
const optional = param.endsWith('?');
163156
param = param.replace(/\?$/, '');
164157

165158
let schema;
166159

167-
// The parameter is can be of multiple types / constants
160+
// Generate a schema for a single parameter that can be of multiple
161+
// types / constants, i.e. `String|Number|Array`.
162+
//
163+
// Here, z.union() is used over z.enum() (which seems more intuitive) for
164+
// constants for the following reasons:
165+
// 1) z.enum() only allows a fixed set of allowable string values. However,
166+
// our constants sometimes have numeric or non-primitive values.
167+
// 2) In some cases, the type can be constants or strings, making z.enum()
168+
// insufficient for the use case.
168169
if (param.includes('|')) {
169170
const types = param.split('|');
170-
171-
// Note that for parameter types that are constants, such as for
172-
// `blendMode`, the parameters are always all caps, sometimes with
173-
// underscores, separated by `|` (i.e. "BLEND|DARKEST|LIGHTEST").
174-
// Use a regex check here to filter them out and distinguish them from
175-
// parameters that allow multiple types.
176-
schema = types.every(t => /^[A-Z_]+$/.test(t))
177-
? generateConstantSchema(types)
178-
: generateUnionSchema(types);
171+
schema = z.union(types
172+
.map(t => generateTypeSchema(t))
173+
.filter(s => s !== undefined));
179174
} else {
180-
schema = schemaMap[param];
175+
schema = generateTypeSchema(param);
181176
}
182177

183178
return optional ? schema.optional() : schema;

0 commit comments

Comments
 (0)