Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed bun.lockb
Binary file not shown.
55 changes: 55 additions & 0 deletions src/compileValueSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
OpenAPINumberSchema,
OpenAPIObjectSchema,
OpenAPIOneOfSchema,
OpenAPIPropertyNamesSchema,
OpenAPIStringSchema,
OpenAPIValueSchema,
} from './types';
Expand Down Expand Up @@ -71,6 +72,17 @@ export function compileValueSchema(compiler: Compiler, schema: OpenAPIValueSchem
return compileAnySchema(compiler, schema);
}

function normalizePropertyNamesSchema(schema: OpenAPIPropertyNamesSchema): OpenAPIValueSchema {
if ('type' in schema || 'anyOf' in schema || 'oneOf' in schema || 'allOf' in schema) {
return schema;
}

return {
type: 'string',
...schema,
};
}

function compileAnyOfSchema(compiler: Compiler, schema: OpenAPIAnyOfSchema) {
return compiler.declareValidationFunction(schema, ({ value, path, context, error }) => {
const nodes: namedTypes.BlockStatement['body'] = [];
Expand Down Expand Up @@ -337,6 +349,49 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
]),
);

if (schema.propertyNames) {
const keyIdentifier = builders.identifier('key');
const keyResultIdentifier = builders.identifier('keyResult');
const propertyNamesSchemaIdentifier = compileValueSchema(
compiler,
normalizePropertyNamesSchema(schema.propertyNames),
);

nodes.push(
builders.forOfStatement(
builders.variableDeclaration('const', [
builders.variableDeclarator(keyIdentifier),
]),
keysIdentifier,
builders.blockStatement([
builders.variableDeclaration('const', [
builders.variableDeclarator(
keyResultIdentifier,
builders.callExpression(propertyNamesSchemaIdentifier, [
builders.arrayExpression([
builders.spreadElement(path),
keyIdentifier,
]),
keyIdentifier,
context,
]),
),
]),
builders.ifStatement(
builders.binaryExpression(
'instanceof',
keyResultIdentifier,
ValidationErrorIdentifier,
),
builders.blockStatement([
builders.returnStatement(keyResultIdentifier),
]),
),
]),
),
);
}

if (schema.minProperties) {
nodes.push(
builders.ifStatement(
Expand Down
1 change: 1 addition & 0 deletions src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const PRESERVE_PROPS = [
'pattern',
'format',
'properties',
'propertyNames',
'additionalProperties',
'minProperties',
'maxProperties',
Expand Down
Loading