MCPcopy
hub / github.com/processing/p5.js / generateZodSchemasForFunc

Function generateZodSchemasForFunc

src/core/friendly_errors/param_validator.js:143–297  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

141 * @returns {z.ZodSchema} Zod schema
142 */
143 const generateZodSchemasForFunc = function (func) {
144 const { funcName, funcClass } = extractFuncNameAndClass(func);
145 let funcInfo = dataDoc[funcClass][funcName];
146
147 if(!funcInfo) return;
148
149 let overloads = [];
150 if (funcInfo.hasOwnProperty('overloads')) {
151 overloads = funcInfo.overloads;
152 }
153
154 // Returns a schema for a single type, i.e. z.boolean() for `boolean`.
155 const generateTypeSchema = baseType => {
156 if (!baseType) return z.any();
157
158 let typeSchema;
159
160 // Check for constants. Note that because we're ultimately interested in the value of
161 // the constant, mapping constants to their values via `constantsMap` is
162 // necessary.
163 if (baseType in constantsMap) {
164 typeSchema = z.literal(constantsMap[baseType]);
165 }
166 // Some more constants are attached directly to p5.prototype, e.g. by addons:
167 else if (baseType.match(/^[A-Z][A-Z0-9]*$/) && baseType in fn) {
168 typeSchema = z.literal(fn[baseType]);
169 }
170 // Function types
171 else if (baseType.startsWith('function')) {
172 typeSchema = z.function();
173 }
174 // All p5 objects start with `p5` in the documentation, i.e. `p5.Camera`.
175 else if (/^p5\.[a-zA-Z0-9]+$/.exec(baseType) || baseType === 'p5') {
176 const className = baseType.substring(baseType.indexOf('.') + 1);
177 typeSchema = z.instanceof(p5Constructors[className]);
178 }
179 // For primitive types and web API objects.
180 else if (schemaMap[baseType]) {
181 typeSchema = schemaMap[baseType];
182 }
183 // Tuple types
184 else if (
185 baseType.startsWith('[') &&
186 baseType.endsWith(']') &&
187 validBracketNesting(baseType.slice(1, -1))
188 ) {
189 typeSchema = z.tuple(
190 baseType
191 .slice(1, -1)
192 .split(/, */g)
193 .map(entry => generateTypeSchema(entry))
194 );
195 }
196 // JavaScript classes, e.g. Request
197 else if (baseType.match(/^[A-Z]/) && baseType in window) {
198 typeSchema = z.instanceof(window[baseType]);
199 }
200 // Generate a schema for a single parameter that can be of multiple

Callers 1

validateFunction · 0.85

Calls 6

extractFuncNameAndClassFunction · 0.85
generateParamSchemaFunction · 0.85
atMethod · 0.80
filterMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected