Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for mixing fixed and additionalProps #26

Merged
merged 2 commits into from
Nov 22, 2024
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
9 changes: 8 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class V1Validator {
/^#\/exports$/, // allow defining exports named `type` or `format`
/^#\/imports$/ // allow defining imports named `type` or `format`
]

const shouldValidate = skipPatterns.none(pattern => pattern.test(currentPath))
if (shouldValidate) {
this.validateTypedInterface(node)
Expand Down Expand Up @@ -137,6 +137,13 @@ class V1Validator {
}
}

// TODO consider adding properties to XtpTyped when we support inlining objects
if ('properties' in prop && Object.keys(prop.properties!).length > 0) {
if (prop.additionalProperties) {
this.recordError('We currently do not support objects with both fixed properties and additionalProperties')
}
}

if (prop.items) this.validateTypedInterface(prop.items)
if (prop.additionalProperties) this.validateTypedInterface(prop.additionalProperties)
}
Expand Down
17 changes: 17 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ test('parse-v1-invalid-identifiers-doc', () => {
}
})

test('parse-v1-additional-props-doc', () => {
const invalidV1Doc: any = yaml.load(fs.readFileSync('./tests/schemas/v1-invalid-additional-properties.yaml', 'utf8'))
try {
parse(JSON.stringify(invalidV1Doc))
expect(true).toBe('should have thrown')
} catch (e) {
const expectedErrors = [
{
message: 'We currently do not support objects with both fixed properties and additionalProperties',
path: '#/components/schemas/MixedObject'
},
]

expectErrors(e, expectedErrors)
}
})

function expectErrors(e: any, expectedErrors: ValidationError[]) {
if (e instanceof NormalizeError) {
const sortByPath = (a: ValidationError, b: ValidationError) => a.path.localeCompare(b.path);
Expand Down
14 changes: 14 additions & 0 deletions tests/schemas/v1-invalid-additional-properties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: v1-draft
exports:
hello: {}
components:
schemas:
MixedObject:
description: should not allow mixing fixed and additional props for now
properties:
hello:
type: string
additionalProperties:
type: string

Loading