Skip to content

Commit

Permalink
fix: handle {} types (#743)
Browse files Browse the repository at this point in the history
According to the JSON Schema spec, `{}` should be treated as the `any`
type.
  • Loading branch information
rix0rrr authored Dec 5, 2023
1 parent ae0d542 commit d1954b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,15 @@
"type": "object"
},
"jsonschema.AnyType": {
"const": true,
"type": "boolean"
"anyOf": [
{
"const": true,
"type": "boolean"
},
{
"$ref": "#/definitions/jsonschema.EmptyObject"
}
]
},
"jsonschema.Boolean": {
"additionalProperties": false,
Expand Down Expand Up @@ -446,6 +453,12 @@
}
]
},
"jsonschema.EmptyObject": {
"additionalProperties": {
"not": {}
},
"type": "object"
},
"jsonschema.MapLikeObject": {
"additionalProperties": false,
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@
"type": "object"
},
"jsonschema.AnyType": {
"const": true,
"type": "boolean"
"anyOf": [
{
"const": true,
"type": "boolean"
},
{
"$ref": "#/definitions/jsonschema.EmptyObject"
}
]
},
"jsonschema.Boolean": {
"additionalProperties": false,
Expand Down Expand Up @@ -108,6 +115,12 @@
}
]
},
"jsonschema.EmptyObject": {
"additionalProperties": {
"not": {}
},
"type": "object"
},
"jsonschema.MapLikeObject": {
"additionalProperties": false,
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ export namespace jsonschema {

export type ConcreteSingletonSchema = Object | String | SchemaArray | Boolean | Number | Null | AnyType;

export type AnyType = true;
export type AnyType = true | EmptyObject;

export type EmptyObject = Record<string, never>;

export function isAnyType(x: Schema): x is AnyType {
return x === true;
return x === true || isEmptyObject(x);
}

function isEmptyObject(x: any) {
return x && typeof x === 'object' && !Array.isArray(x) && Object.keys(x).length === 0;
}

export interface Annotatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,22 @@ test('type definitions in deprecated properties do not fail', () => {

// THEN: no failure
});

test('empty objects are treated as "any"', () => {
importCloudFormationRegistryResource({
db,
report,
resource: {
typeName: 'AWS::Some::Type',
description: 'resource with PrimaryIdentifier',
properties: {
id: { type: 'string' },
data: {},
},
},
});

// THEN:
const resource = db.lookup('resource', 'cloudFormationType', 'equals', 'AWS::Some::Type').only();
expect(resource.properties.data.type).toEqual({ type: 'json' });
});

0 comments on commit d1954b8

Please sign in to comment.