@@ -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+
7486function 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 (
0 commit comments