Skip to content

Commit ccd8b22

Browse files
CLOUDP-283086: POC validator with custom function
1 parent 3d19964 commit ccd8b22

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tools/ipa/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
2+
3+
SPECTRAL_VERSION=${SPECTRAL_VERSION:-6.11.1}
4+
SPECTRAL_CORE_VERSION=${SPECTRAL_CORE_VERSION:-1.19.1}
5+
6+
.PHONY: deps
7+
deps: ## Download go module dependencies
8+
@echo "==> Installing npx..."
9+
npm install -g npx
10+
11+
12+
.PHONY: devtools
13+
devtools: ## Install dev tools
14+
@echo "==> Installing dev tools..."
15+
npm install -g @stoplight/spectral-cli@"${SPECTRAL_VERSION}"
16+
npm install -g @stoplight/spectral-core@"${SPECTRAL_CORE_VERSION}"
17+
18+
.PHONY: setup
19+
setup: deps devtools ## Set up dev env
20+
21+
.PHONY: linterr
22+
linterr: ## Run spectral linter on foas
23+
@echo "==> Running spectral linter"
24+
npx -- @stoplight/spectral-cli@"${SPECTRAL_VERSION}" lint ../../openapi/v2.yaml --ruleset=ruleset.yaml
25+
26+
.PHONY: lintwarn
27+
lintwarn: ## Run spectral linter on foas
28+
@echo "==> Running spectral linter"
29+
npx -- @stoplight/spectral-cli@"${SPECTRAL_VERSION}" lint ../../openapi/v2.yaml --ruleset=ruleset.yaml --fail-severity=warn
30+
31+
.PHONY: help
32+
.DEFAULT_GOAL := help
33+
help:
34+
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default (input, _, { path }) => {
2+
if (!input.description || input.description === "") {
3+
return [
4+
{
5+
// "property" can be used to print the name of the property we are evaluating
6+
message: `#{{print("property")}}MUST have a description.`,
7+
},
8+
];
9+
}
10+
}

tools/ipa/ruleset.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
functions: [hasDescription]
2+
3+
aliases:
4+
PathItem:
5+
- "$.paths[*]"
6+
OperationObject:
7+
- "#PathItem[get,put,post,delete,options,head,patch,trace]"
8+
DescribableObjects:
9+
- "$.info"
10+
- "$.tags[*]"
11+
- "#OperationObject"
12+
- "#PathItem.parameters[?(@ && @.in)]"
13+
- "#OperationObject.parameters[?(@ && @.in)]"
14+
- "$.components.schemas[*].properties[?(@ && @.type)]"
15+
16+
rules:
17+
18+
# IPA-117: Documentation
19+
xgen-description:
20+
description: "API producers must provide descriptions for Properties, Operations and Parameters. http://go/ipa/117"
21+
message: "{{error}} http://go/ipa/117"
22+
severity: error
23+
given: "#DescribableObjects"
24+
then:
25+
function: "hasDescription" # Doesn't have to be custom, just an example
26+
27+
xgen-description-format:
28+
description: "Descriptions must start with Uppercase and end with a full stop(.). http://go/ipa/117"
29+
message: "Description for {{property}} MUST start with Uppercase and end with a full stop(.). http://go/ipa/117"
30+
severity: error
31+
given: "#DescribableObjects"
32+
then:
33+
- field: "description"
34+
function: pattern
35+
functionOptions:
36+
match: "/^[A-Z]/"
37+
- field: "description"
38+
function: pattern
39+
functionOptions:
40+
match: "\\.|\|$"

0 commit comments

Comments
 (0)