-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathschema-validation.ts
38 lines (35 loc) · 1.99 KB
/
schema-validation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as SchemaValidator from 'z-schema';
import { parseJsonPointer } from '../ref/jsonpath';
import { ReadUri, ResolveUri } from '../ref/uri';
import { QuickDataSource } from '../data-store/data-store';
import { Parse } from '../ref/yaml';
import { CreatePerFilePlugin, PipelinePlugin } from "./common";
import { Channel } from "../message";
import { OperationAbortedException } from '../exception';
export function GetPlugin_SchemaValidator(): PipelinePlugin {
const validator = new SchemaValidator({ breakOnFirstError: false });
const extendedSwaggerSchema = require("./swagger-extensions.json");
(validator as any).setRemoteReference("http://json.schemastore.org/swagger-2.0", require("./swagger.json"));
(validator as any).setRemoteReference("https://raw.githubusercontent.com/Azure/autorest/master/schema/example-schema.json", require("./example-schema.json"));
return CreatePerFilePlugin(async config => async (fileIn, sink) => {
const obj = fileIn.ReadObject<any>();
const errors = await new Promise<{ code: string, params: string[], message: string, path: string }[] | null>(res => validator.validate(obj, extendedSwaggerSchema, (err, valid) => res(valid ? null : err)));
if (errors !== null) {
for (const error of errors) {
config.Message({
Channel: Channel.Error,
Details: error,
Plugin: "schema-validator",
Source: [{ document: fileIn.key, Position: { path: parseJsonPointer(error.path) } as any }],
Text: `Schema violation: ${error.message}`
});
}
throw new OperationAbortedException();
}
return await sink.Forward(fileIn.Description, fileIn);
});
}