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}); 43 44const testResultSchema: yup.ObjectSchema<TestResult> = yup.object({ 45 result: yup. 46 array( 47 yup.object({ 48 line: yup.number().required(), 49 column: yup.number().required(), 50 endLine: yup.number().required(), 51 endColumn: yup.number().required(), 52 problem: yup.string().required(), 53 suggest: yup.string().defined(), 54 rule: yup.string().required(), 55 severity: yup.string().required(), 56 exclusive: yup.string(), 57 autofix: yup.array(autofixResultSchema) 58 }) 59 ). 60 required() 61}); 62 63export function validateTestResult(value: object): TestResult { 64 try { 65 return testResultSchema.validateSync(value, { strict: true, abortEarly: false }); 66 } catch (error) { 67 if (error instanceof yup.ValidationError) { 68 let errMsg = ''; 69 for (const msg of error.errors) { 70 errMsg += '\n' + TAB + TAB + msg; 71 } 72 throw new Error(errMsg); 73 } 74 throw error; 75 } 76} 77