Fix review feedback: override warning, input guard, required tests
Pass merged config schemas via configSchema instead of config.schema in the override method to avoid false deprecation warnings. Add type guard for non-object inputs in parse. Add tests for defaulted field required semantics (already correct) and invalid input types. Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com> Made-with: Cursor
This commit is contained in:
@@ -349,5 +349,36 @@ describe('createConfigSchema', () => {
|
||||
additionalProperties: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw a clear error for non-object input', () => {
|
||||
const schema = createConfigSchema({ title: z => z.string() });
|
||||
|
||||
expect(() => schema.parse('not an object')).toThrow(
|
||||
'Invalid config input, expected object but got string',
|
||||
);
|
||||
expect(() => schema.parse(42)).toThrow(
|
||||
'Invalid config input, expected object but got number',
|
||||
);
|
||||
expect(() => schema.parse([1, 2])).toThrow(
|
||||
'Invalid config input, expected object but got array',
|
||||
);
|
||||
expect(() => schema.parse(true)).toThrow(
|
||||
'Invalid config input, expected object but got boolean',
|
||||
);
|
||||
});
|
||||
|
||||
it('should not mark defaulted zod v3 fields as required in JSON Schema', () => {
|
||||
const schema = createConfigSchema({
|
||||
name: z => z.string(),
|
||||
title: z => z.string().default('hello'),
|
||||
count: z => z.number().optional(),
|
||||
});
|
||||
|
||||
const result = schema.schema();
|
||||
expect(result.schema).toMatchObject({
|
||||
type: 'object',
|
||||
required: ['name'],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -100,6 +100,17 @@ function buildPortableSchema<TOutput = unknown>(
|
||||
fields: Record<string, ResolvedField>,
|
||||
): MergeablePortableSchema<TOutput> {
|
||||
function parse(input: unknown) {
|
||||
if (
|
||||
input !== undefined &&
|
||||
input !== null &&
|
||||
(typeof input !== 'object' || Array.isArray(input))
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid config input, expected object but got ${
|
||||
Array.isArray(input) ? 'array' : typeof input
|
||||
}`,
|
||||
);
|
||||
}
|
||||
const inputObj = (input ?? {}) as Record<string, unknown>;
|
||||
const result: Record<string, unknown> = {};
|
||||
const errors: string[] = [];
|
||||
|
||||
@@ -726,18 +726,16 @@ export function createExtension(
|
||||
),
|
||||
output: (overrideOptions.output ??
|
||||
options.output) as ExtensionDataRef[],
|
||||
config:
|
||||
configSchema:
|
||||
options.config ||
|
||||
options.configSchema ||
|
||||
overrideOptions.config ||
|
||||
overrideOptions.configSchema
|
||||
? {
|
||||
schema: {
|
||||
...options.config?.schema,
|
||||
...options.configSchema,
|
||||
...overrideOptions.config?.schema,
|
||||
...overrideOptions.configSchema,
|
||||
},
|
||||
...options.config?.schema,
|
||||
...options.configSchema,
|
||||
...overrideOptions.config?.schema,
|
||||
...overrideOptions.configSchema,
|
||||
}
|
||||
: undefined,
|
||||
factory: ({ node, apis, config, inputs }) => {
|
||||
|
||||
Reference in New Issue
Block a user