Skip to content

Commit

Permalink
Always set additionalProperties (#10)
Browse files Browse the repository at this point in the history
This is done to make it easier to distinguish whether additional properties are allowed or not in a schema.

Note: additional properties are always ignored by terraform, and will create a warning if any are set.

External behaviour of the schemas themselves are not changed since `additionalProperties: true` is equivalent to not setting it.
  • Loading branch information
AislingHPE authored Aug 23, 2024
1 parent 59448ae commit e95a8b5
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/jsonschema/json-schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func CreateSchema(path string, strict bool) (map[string]any, error) {

if strict {
schemaOut["additionalProperties"] = false
} else {
schemaOut["additionalProperties"] = true
}

properties := make(map[string]any)
Expand Down
2 changes: 2 additions & 0 deletions pkg/jsonschema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func getObject(in []any, strict bool) (map[string]any, error) {
}
if strict {
node["additionalProperties"] = false
} else {
node["additionalProperties"] = true
}

if len(in) != 2 && len(in) != 3 {
Expand Down
4 changes: 4 additions & 0 deletions test/expected/complex-types/schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": true,
"properties": {
"a_very_complicated_object": {
"additionalProperties": true,
"default": {
"b": [
[
Expand Down Expand Up @@ -80,6 +82,7 @@
"type": "object"
},
"d": {
"additionalProperties": true,
"properties": {
"a": {
"items": {
Expand Down Expand Up @@ -134,6 +137,7 @@
"type": "object"
},
"an_object_with_optional": {
"additionalProperties": true,
"default": {
"a": "a",
"b": 1,
Expand Down
2 changes: 2 additions & 0 deletions test/expected/custom-validation/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": true,
"properties": {
"a_list_maximum_minimum_length": {
"default": [
Expand Down Expand Up @@ -164,6 +165,7 @@
"type": "string"
},
"an_object_maximum_minimum_items": {
"additionalProperties": true,
"default": {
"name": "a",
"other_field": "b"
Expand Down
2 changes: 2 additions & 0 deletions test/expected/simple-types/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": true,
"properties": {
"a_bool": {
"default": false,
Expand Down Expand Up @@ -94,6 +95,7 @@
"type": "string"
},
"an_object": {
"additionalProperties": true,
"default": {
"a": "a",
"b": 1,
Expand Down
1 change: 1 addition & 0 deletions test/expected/simple/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": true,
"properties": {
"age": {
"description": "Your age. Required.",
Expand Down

0 comments on commit e95a8b5

Please sign in to comment.