Skip to content

Commit fc4f1e2

Browse files
committed
Add support for propertyNames
1 parent 87d68a2 commit fc4f1e2

6 files changed

Lines changed: 520 additions & 358 deletions

File tree

src/compileValueSchema.ts

Lines changed: 60 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,22 @@ export function compileValueSchema(compiler: Compiler, schema: OpenAPIValueSchem
7172
return compileAnySchema(compiler, schema);
7273
}
7374

75+
function normalizePropertyNamesSchema(schema: OpenAPIPropertyNamesSchema): OpenAPIValueSchema {
76+
if (
77+
'type' in schema ||
78+
'anyOf' in schema ||
79+
'oneOf' in schema ||
80+
'allOf' in schema
81+
) {
82+
return schema;
83+
}
84+
85+
return {
86+
type: 'string',
87+
...schema,
88+
};
89+
}
90+
7491
function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
7592
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
7693
const nodes: namedTypes.BlockStatement['body'] = [];
@@ -337,6 +354,49 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
337354
]),
338355
);
339356

357+
if (schema.propertyNames) {
358+
const keyIdentifier = builders.identifier('key');
359+
const keyResultIdentifier = builders.identifier('keyResult');
360+
const propertyNamesSchemaIdentifier = compileValueSchema(
361+
compiler,
362+
normalizePropertyNamesSchema(schema.propertyNames),
363+
);
364+
365+
nodes.push(
366+
builders.forOfStatement(
367+
builders.variableDeclaration('const', [
368+
builders.variableDeclarator(keyIdentifier),
369+
]),
370+
keysIdentifier,
371+
builders.blockStatement([
372+
builders.variableDeclaration('const', [
373+
builders.variableDeclarator(
374+
keyResultIdentifier,
375+
builders.callExpression(propertyNamesSchemaIdentifier, [
376+
builders.arrayExpression([
377+
builders.spreadElement(path),
378+
keyIdentifier,
379+
]),
380+
keyIdentifier,
381+
context,
382+
]),
383+
),
384+
]),
385+
builders.ifStatement(
386+
builders.binaryExpression(
387+
'instanceof',
388+
keyResultIdentifier,
389+
ValidationErrorIdentifier,
390+
),
391+
builders.blockStatement([
392+
builders.returnStatement(keyResultIdentifier),
393+
]),
394+
),
395+
]),
396+
),
397+
);
398+
}
399+
340400
if (schema.minProperties) {
341401
nodes.push(
342402
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)