Use oneOf for enums when possible (#108)

This commit is contained in:
Adam Leventhal 2021-09-26 10:02:44 -07:00 committed by GitHub
parent dec8bcc3b7
commit 0a1200baac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 302 additions and 276 deletions

View file

@ -2,7 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "DeprecatedEnum",
"deprecated": true,
"anyOf": [
"oneOf": [
{
"type": "string",
"enum": [
@ -24,13 +24,13 @@
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"deprecated_field": {
"deprecated": true,
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
}
}
}

View file

@ -2,7 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "This is the enum's title",
"description": "This is the enum's description.",
"anyOf": [
"oneOf": [
{
"type": "string",
"enum": [

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Adjacent",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -24,17 +24,17 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"StringMap"
]
},
"c": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"t": {
"type": "string",
"enum": [
"StringMap"
]
}
},
"additionalProperties": false
@ -46,14 +46,14 @@
"t"
],
"properties": {
"c": {
"$ref": "#/definitions/UnitStruct"
},
"t": {
"type": "string",
"enum": [
"UnitStructNewType"
]
},
"c": {
"$ref": "#/definitions/UnitStruct"
}
},
"additionalProperties": false
@ -65,14 +65,14 @@
"t"
],
"properties": {
"c": {
"$ref": "#/definitions/Struct"
},
"t": {
"type": "string",
"enum": [
"StructNewType"
]
},
"c": {
"$ref": "#/definitions/Struct"
}
},
"additionalProperties": false
@ -84,12 +84,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Struct"
]
},
"c": {
"type": "object",
"required": [
@ -97,15 +91,21 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
},
"t": {
"type": "string",
"enum": [
"Struct"
]
}
},
"additionalProperties": false
@ -117,12 +117,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Tuple"
]
},
"c": {
"type": "array",
"items": [
@ -136,6 +130,12 @@
],
"maxItems": 2,
"minItems": 2
},
"t": {
"type": "string",
"enum": [
"Tuple"
]
}
},
"additionalProperties": false
@ -162,24 +162,21 @@
"t"
],
"properties": {
"c": {
"type": "integer",
"format": "int32"
},
"t": {
"type": "string",
"enum": [
"WithInt"
]
},
"c": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
@ -187,14 +184,17 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
"UnitStruct": {
"type": "null"
}
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Adjacent",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -23,17 +23,17 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"StringMap"
]
},
"c": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"t": {
"type": "string",
"enum": [
"StringMap"
]
}
}
},
@ -44,14 +44,14 @@
"t"
],
"properties": {
"c": {
"$ref": "#/definitions/UnitStruct"
},
"t": {
"type": "string",
"enum": [
"UnitStructNewType"
]
},
"c": {
"$ref": "#/definitions/UnitStruct"
}
}
},
@ -62,14 +62,14 @@
"t"
],
"properties": {
"c": {
"$ref": "#/definitions/Struct"
},
"t": {
"type": "string",
"enum": [
"StructNewType"
]
},
"c": {
"$ref": "#/definitions/Struct"
}
}
},
@ -80,12 +80,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Struct"
]
},
"c": {
"type": "object",
"required": [
@ -93,14 +87,20 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
"t": {
"type": "string",
"enum": [
"Struct"
]
}
}
},
@ -111,12 +111,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Tuple"
]
},
"c": {
"type": "array",
"items": [
@ -130,6 +124,12 @@
],
"maxItems": 2,
"minItems": 2
},
"t": {
"type": "string",
"enum": [
"Tuple"
]
}
}
},
@ -154,23 +154,20 @@
"t"
],
"properties": {
"c": {
"type": "integer",
"format": "int32"
},
"t": {
"type": "string",
"enum": [
"WithInt"
]
},
"c": {
"type": "integer",
"format": "int32"
}
}
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
@ -178,14 +175,17 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
"UnitStruct": {
"type": "null"
}
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "External",
"anyOf": [
"oneOf": [
{
"type": "string",
"enum": [
@ -61,12 +61,12 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
@ -112,9 +112,6 @@
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
@ -122,14 +119,17 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
"UnitStruct": {
"type": "null"
}
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "External",
"anyOf": [
"oneOf": [
{
"type": "string",
"enum": [
@ -61,12 +61,12 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
}
@ -111,9 +111,6 @@
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
@ -121,14 +118,17 @@
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
"UnitStruct": {
"type": "null"
}
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Internal",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -55,18 +55,18 @@
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
}
}
},
@ -78,18 +78,18 @@
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
}
},
"additionalProperties": false

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Internal",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -55,18 +55,18 @@
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
}
}
},
@ -78,18 +78,18 @@
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
}
}
},

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "OuterEnum",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Adjacent",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -9,12 +9,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Struct"
]
},
"c": {
"type": "object",
"required": [
@ -25,6 +19,12 @@
"type": "boolean"
}
}
},
"t": {
"type": "string",
"enum": [
"Struct"
]
}
}
},
@ -35,14 +35,14 @@
"t"
],
"properties": {
"c": {
"type": "boolean"
},
"t": {
"type": "string",
"enum": [
"NewType"
]
},
"c": {
"type": "boolean"
}
}
},
@ -53,12 +53,6 @@
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Tuple"
]
},
"c": {
"type": "array",
"items": [
@ -72,6 +66,12 @@
],
"maxItems": 2,
"minItems": 2
},
"t": {
"type": "string",
"enum": [
"Tuple"
]
}
}
},
@ -82,14 +82,14 @@
"t"
],
"properties": {
"c": {
"type": "boolean"
},
"t": {
"type": "string",
"enum": [
"Unit"
]
},
"c": {
"type": "boolean"
}
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "External",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Internal",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
@ -9,14 +9,14 @@
"typeProperty"
],
"properties": {
"foo": {
"type": "boolean"
},
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
},
"foo": {
"type": "boolean"
}
}
},

View file

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyEnum",
"anyOf": [
"oneOf": [
{
"type": "string",
"enum": [

View file

@ -22,7 +22,7 @@ pub fn test_schema(actual: &RootSchema, file: &str) -> TestResult {
let expected_json = match fs::read_to_string(format!("tests/expected/{}.json", file)) {
Ok(j) => j,
Err(e) => {
write_actual_to_file(&actual, file)?;
write_actual_to_file(actual, file)?;
return Err(Box::from(e));
}
};
@ -32,7 +32,7 @@ pub fn test_schema(actual: &RootSchema, file: &str) -> TestResult {
write_actual_to_file(actual, file)?;
}
assert_eq!(actual, expected);
assert_eq!(expected, actual);
Ok(())
}