Skip to content

Commit 23ab359

Browse files
New Spectral Function Added: Ensuring Schema property has type defined (#1053)
* New Spectral Function Added: Ensuring Schema property has type defined * New Spectral Function Added: Ensuring Schema property has type defined
1 parent 222b6c3 commit 23ab359

File tree

9 files changed

+62
-0
lines changed

9 files changed

+62
-0
lines changed

specification/resources/apps/models/app_log_destination_open_search_spec_basic_auth.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ properties:
77
Defaults to `doadmin` when `cluster_name` is set.
88
example: apps_user
99
password:
10+
type: string
1011
description: |-
1112
Password for user defined in User. Is required when `endpoint` is set.
1213
Cannot be set if using a DigitalOcean DBaaS OpenSearch cluster.

specification/resources/droplets/responses/all_droplet_backup_policies.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ content:
1717
- type: object
1818
properties:
1919
policies:
20+
type: object
2021
description: |
2122
A map where the keys are the Droplet IDs and the values are
2223
objects containing the backup policy information for each Droplet.

specification/resources/kubernetes/models/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ kubernetes_options:
33
type: object
44
properties:
55
options:
6+
type: object
67
properties:
78
regions:
89
type: array

specification/resources/monitoring/models/destination.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ properties:
1111
description: "destination name"
1212
example: "managed_opensearch_cluster"
1313
type:
14+
type: string
1415
enum:
1516
- opensearch_dbaas
1617
- opensearch_ext

specification/resources/monitoring/models/destination_omit_credentials.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ properties:
99
description: "destination name"
1010
example: "managed_opensearch_cluster"
1111
type:
12+
type: string
1213
enum:
1314
- opensearch_dbaas
1415
- opensearch_ext

specification/resources/monitoring/models/destination_request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ properties:
88
description: "destination name"
99
example: "managed_opensearch_cluster"
1010
type:
11+
type: string
1112
enum:
1213
- opensearch_dbaas
1314
- opensearch_ext

specification/resources/regions/models/region.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ properties:
1414
example: nyc3
1515

1616
features:
17+
type: array
1718
items:
1819
type: string
1920
description: This attribute is set to an array which contains features available
@@ -34,6 +35,7 @@ properties:
3435
example: true
3536

3637
sizes:
38+
type: array
3739
items:
3840
type: string
3941
description: This attribute is set to an array which contains the identifying
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
*
3+
* Ensures that every schema property has a type defined
4+
*
5+
* This function validates that all schema properties include a 'type' field,
6+
* unless they have other valid schema indicators like $ref, allOf, anyOf, oneOf
7+
*/
8+
export default (input, _, context) => {
9+
if (!input || typeof input !== "object") {
10+
return;
11+
}
12+
13+
if (input["$ref"]) {
14+
return;
15+
}
16+
17+
if (input["allOf"] || input["anyOf"] || input["oneOf"]) {
18+
return;
19+
}
20+
21+
if (context.path.join(".").endsWith(".properties")) {
22+
return;
23+
}
24+
25+
const pathString = context.path.join(".");
26+
if (pathString.includes(".items.") && !pathString.endsWith(".items")) {
27+
return;
28+
}
29+
30+
const hasSchemaLikeProperties =
31+
input.description ||
32+
input.example ||
33+
input.items ||
34+
input.properties ||
35+
input.enum ||
36+
input.format ||
37+
input.minimum ||
38+
input.maximum ||
39+
input.minLength ||
40+
input.maxLength;
41+
42+
if (hasSchemaLikeProperties && !input.type) {
43+
return [ {message: `Schema property is missing 'type' field. Path: ${context.path.join(".")}`,},];
44+
}
45+
};

spectral/ruleset.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ functions:
33
- ensureAllArraysHaveItemTypes
44
- ensureSnakeCaseWithDigits
55
- validateOpIDNaming
6+
- ensureSchemaHasType
67

78
rules:
89
ratelimit-headers:
@@ -175,6 +176,14 @@ rules:
175176
then:
176177
function: ensureSnakeCaseWithDigits
177178

179+
schema-properties-must-have-type:
180+
description: Schema properties must have a type defined
181+
given: '$..properties.*'
182+
severity: error
183+
message: '{{error}}'
184+
then:
185+
function: ensureSchemaHasType
186+
178187
oas3-operation-security-defined:
179188
description: Check operation security is defined
180189
severity: "error"

0 commit comments

Comments
 (0)