Skip to content

Commit 258d62f

Browse files
authored
Add support for propertyNames in object type (#8)
* Switch to text lockfile * Add support for propertyNames * Format
1 parent 28fa5af commit 258d62f

8 files changed

Lines changed: 566 additions & 358 deletions

File tree

bun.lock

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bun.lockb

-6.04 KB
Binary file not shown.

src/compileValueSchema.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
OpenAPINumberSchema,
1313
OpenAPIObjectSchema,
1414
OpenAPIOneOfSchema,
15+
OpenAPIPropertyNamesSchema,
1516
OpenAPIStringSchema,
1617
OpenAPIValueSchema,
1718
} from './types';
@@ -71,6 +72,17 @@ export function compileValueSchema(compiler: Compiler, schema: OpenAPIValueSchem
7172
return compileAnySchema(compiler, schema);
7273
}
7374

75+
function normalizePropertyNamesSchema(schema: OpenAPIPropertyNamesSchema): OpenAPIValueSchema {
76+
if ('type' in schema || 'anyOf' in schema || 'oneOf' in schema || 'allOf' in schema) {
77+
return schema;
78+
}
79+
80+
return {
81+
type: 'string',
82+
...schema,
83+
};
84+
}
85+
7486
function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
7587
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
7688
const nodes: namedTypes.BlockStatement['body'] = [];
@@ -337,6 +349,49 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
337349
]),
338350
);
339351

352+
if (schema.propertyNames) {
353+
const keyIdentifier = builders.identifier('key');
354+
const keyResultIdentifier = builders.identifier('keyResult');
355+
const propertyNamesSchemaIdentifier = compileValueSchema(
356+
compiler,
357+
normalizePropertyNamesSchema(schema.propertyNames),
358+
);
359+
360+
nodes.push(
361+
builders.forOfStatement(
362+
builders.variableDeclaration('const', [
363+
builders.variableDeclarator(keyIdentifier),
364+
]),
365+
keysIdentifier,
366+
builders.blockStatement([
367+
builders.variableDeclaration('const', [
368+
builders.variableDeclarator(
369+
keyResultIdentifier,
370+
builders.callExpression(propertyNamesSchemaIdentifier, [
371+
builders.arrayExpression([
372+
builders.spreadElement(path),
373+
keyIdentifier,
374+
]),
375+
keyIdentifier,
376+
context,
377+
]),
378+
),
379+
]),
380+
builders.ifStatement(
381+
builders.binaryExpression(
382+
'instanceof',
383+
keyResultIdentifier,
384+
ValidationErrorIdentifier,
385+
),
386+
builders.blockStatement([
387+
builders.returnStatement(keyResultIdentifier),
388+
]),
389+
),
390+
]),
391+
),
392+
);
393+
}
394+
340395
if (schema.minProperties) {
341396
nodes.push(
342397
builders.ifStatement(

src/hash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const PRESERVE_PROPS = [
2323
'pattern',
2424
'format',
2525
'properties',
26+
'propertyNames',
2627
'additionalProperties',
2728
'minProperties',
2829
'maxProperties',

0 commit comments

Comments
 (0)