1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as yup from 'yup'; 17import type { Autofix } from '../lib/autofixes/Autofixer'; 18 19const TAB = ' '; 20 21export interface TestResult { 22 result: TestProblemInfo[]; 23} 24 25export interface TestProblemInfo { 26 line: number; 27 column: number; 28 endLine: number; 29 endColumn: number; 30 problem: string; 31 suggest: string; 32 rule: string; 33 severity: string; 34 exclusive?: string; 35 autofix?: Autofix[]; 36} 37 38const autofixResultSchema: yup.ObjectSchema<Autofix> = yup.object({ 39 replacementText: yup.string().defined(), 40 start: yup.number().required(), 41 end: yup.number().required(), 42 line: yup.number().optional(), 43 column: yup.number().optional(), 44 endLine: yup.number().optional(), 45 endColumn: yup.number().optional() 46}); 47 48const testResultSchema: yup.ObjectSchema<TestResult> = yup.object({ 49 result: yup. 50 array( 51 yup.object({ 52 line: yup.number().required(), 53 column: yup.number().required(), 54 endLine: yup.number().required(), 55 endColumn: yup.number().required(), 56 problem: yup.string().required(), 57 suggest: yup.string().defined(), 58 rule: yup.string().required(), 59 severity: yup.string().required(), 60 exclusive: yup.string(), 61 autofix: yup.array(autofixResultSchema) 62 }) 63 ). 64 required() 65}); 66 67export function validateTestResult(value: object): TestResult { 68 try { 69 return testResultSchema.validateSync(value, { strict: true, abortEarly: false }); 70 } catch (error) { 71 if (error instanceof yup.ValidationError) { 72 let errMsg = ''; 73 for (const msg of error.errors) { 74 errMsg += '\n' + TAB + TAB + msg; 75 } 76 throw new Error(errMsg); 77 } 78 throw error; 79 } 80} 81