-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate _superseded_operations.yaml against its JSON schema (#276)
* Validate _superseded_operations.yaml against its JSON schema Signed-off-by: Theo Truong <theotr@amazon.com> * # lint Signed-off-by: Theo Truong <theotr@amazon.com> * # lint Signed-off-by: Theo Truong <theotr@amazon.com> --------- Signed-off-by: Theo Truong <theotr@amazon.com>
- Loading branch information
Showing
10 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
$schema: http://json-schema.org/draft-07/schema# | ||
type: object | ||
patternProperties: | ||
^\$schema$: | ||
type: string | ||
^/: | ||
type: object | ||
properties: | ||
superseded_by: | ||
type: string | ||
pattern: ^/ | ||
operations: | ||
type: array | ||
items: | ||
type: string | ||
enum: [GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH] | ||
required: [superseded_by, operations] | ||
additionalProperties: false | ||
required: [$schema] | ||
additionalProperties: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import FileValidator from './base/FileValidator' | ||
import ajv from 'ajv' | ||
import fs from 'fs' | ||
import YAML from 'yaml' | ||
import { type ValidationError } from '../../types' | ||
|
||
export default class SupersededOperationsFile extends FileValidator { | ||
JSON_SCHEMA_PATH = '../json_schemas/_superseded_operations.yaml' | ||
|
||
validate (): ValidationError[] { | ||
return [ | ||
this.validate_json_schema() | ||
].filter(e => e) as ValidationError[] | ||
} | ||
|
||
validate_json_schema (): ValidationError | undefined { | ||
const schema = YAML.parse(fs.readFileSync(this.JSON_SCHEMA_PATH, 'utf8')) | ||
const validator = (new ajv()).compile(schema) | ||
if (!validator(this.spec())) { | ||
return this.error(`File content does not match JSON schema found in '${this.JSON_SCHEMA_PATH}':\n ${JSON.stringify(validator.errors, null, 2)}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import SupersededOperationsFile from '../../linter/components/SupersededOperationsFile' | ||
|
||
test('validate()', () => { | ||
const validator = new SupersededOperationsFile('./test/linter/fixtures/_superseded_operations.yaml') | ||
expect(validator.validate()).toEqual([ | ||
{ | ||
file: 'fixtures/_superseded_operations.yaml', | ||
message: "File content does not match JSON schema found in '../json_schemas/_superseded_operations.yaml':\n [\n {\n \"instancePath\": \"/~1hello~1world/operations/1\",\n \"schemaPath\": \"#/patternProperties/%5E~1/properties/operations/items/enum\",\n \"keyword\": \"enum\",\n \"params\": {\n \"allowedValues\": [\n \"GET\",\n \"POST\",\n \"PUT\",\n \"DELETE\",\n \"HEAD\",\n \"OPTIONS\",\n \"PATCH\"\n ]\n },\n \"message\": \"must be equal to one of the allowed values\"\n }\n]" | ||
} | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
$schema: ../../../../json_schemas/_superseded_operations.yaml | ||
|
||
/hello/world: | ||
superseded_by: /goodbye/world | ||
operations: | ||
- GET | ||
- CLEAN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$schema: ../../../../../json_schemas/_superseded_operations.yaml |