-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added StoryValidator #362
Added StoryValidator #362
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ export default class SchemaValidator { | |
validate (schema: OpenAPIV3.SchemaObject, data: any): Evaluation { | ||
const validate = this.ajv.compile(schema) | ||
const valid = validate(data) | ||
if (! valid) { | ||
if (!valid) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing lint? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue with the previous PR: the rule was moved into a plugin. |
||
this.logger.info(`# ${to_json(schema)}`) | ||
this.logger.info(`* ${to_json(data)}`) | ||
this.logger.info(`& ${to_json(validate.errors)}`) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Result, StoryEvaluation, StoryFile } from "./types/eval.types"; | ||
import * as path from "path"; | ||
import { Ajv2019, ValidateFunction } from 'ajv/dist/2019' | ||
import addFormats from 'ajv-formats' | ||
import { read_yaml } from "../helpers"; | ||
|
||
export default class StoryValidator { | ||
private static readonly SCHEMA_FILE = path.resolve("./json_schemas/test_story.schema.yaml") | ||
private readonly ajv: Ajv2019 | ||
private readonly validate_schema: ValidateFunction | ||
|
||
constructor() { | ||
this.ajv = new Ajv2019({ allErrors: true, strict: false }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use plain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because of the use of unevaluatedProperties, which is a new feature in JSON Schema draft 2019-09. We use this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we upgrade all schema checks? |
||
addFormats(this.ajv) | ||
const schema = read_yaml(StoryValidator.SCHEMA_FILE) | ||
this.validate_schema = this.ajv.compile(schema) | ||
} | ||
|
||
validate(story_file: StoryFile): StoryEvaluation | undefined { | ||
const schema_file_error = this.#validate_schema_path(story_file) | ||
if (schema_file_error != null) return schema_file_error | ||
const schema_error = this.#validate_schema(story_file) | ||
if (schema_error != null) return schema_error | ||
} | ||
|
||
#validate_schema_path(story_file: StoryFile): StoryEvaluation | undefined { | ||
const actual_path = story_file.story.$schema | ||
const expected_path = path.relative(path.dirname(story_file.full_path), StoryValidator.SCHEMA_FILE) | ||
if (actual_path !== expected_path) return this.#invalid(story_file, `Expected $schema to be ${expected_path}`) | ||
} | ||
|
||
#validate_schema(story_file: StoryFile): StoryEvaluation | undefined { | ||
const valid = this.validate_schema(story_file.story) | ||
if (!valid) return this.#invalid(story_file, this.ajv.errorsText(this.validate_schema.errors)) | ||
} | ||
|
||
#invalid({ story, display_path, full_path }: StoryFile, message: string): StoryEvaluation { | ||
return { | ||
display_path, | ||
full_path, | ||
description: story.description, | ||
result: Result.ERROR, | ||
message: `Invalid Story: ${message}`, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this return an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In prologues and epilogues, we don't evaluate any spec but rather simply run the HTTP requests to setup and tear down. By default, the framework accepts
status: [200, 201]
as okay and other codes will make the prologue or epilogue return an error. So in this case in particular, we could have omittedstatus
. Specifying status is usually for when cleanup where we want to delete a resource created in one of the chapters but we want to account for 404 where the resource was not created successfully. In this case, it's usuallystatus: [200, 404]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#299 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added unit tests for StoryValidator