diff --git a/package-lock.json b/package-lock.json index 63aab739..a0ac4be5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "chalk": "^4.1.2", "event-stream": "4.0.1", "form-data": "4.0.0", - "har-schema": "^2.0.0", + "har-validator-compiled": "^1.0.0", "stringify-object": "3.3.0", "yargs": "^17.4.0" }, @@ -2721,12 +2721,11 @@ "dev": true, "license": "ISC" }, - "node_modules/har-schema": { - "version": "2.0.0", - "license": "ISC", - "engines": { - "node": ">=4" - } + "node_modules/har-validator-compiled": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/har-validator-compiled/-/har-validator-compiled-1.0.0.tgz", + "integrity": "sha512-dher7nFSx+Ef6OoqVveLClh8itAR3vd8Qx70Lh/hEgP1iGeARAolbci7Y8JBrHIYgFCT6xRdvvL16AR9Zh07Dw==", + "license": "MIT" }, "node_modules/has": { "version": "1.0.3", @@ -7100,8 +7099,10 @@ "version": "4.2.9", "dev": true }, - "har-schema": { - "version": "2.0.0" + "har-validator-compiled": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/har-validator-compiled/-/har-validator-compiled-1.0.0.tgz", + "integrity": "sha512-dher7nFSx+Ef6OoqVveLClh8itAR3vd8Qx70Lh/hEgP1iGeARAolbci7Y8JBrHIYgFCT6xRdvvL16AR9Zh07Dw==" }, "has": { "version": "1.0.3", diff --git a/package.json b/package.json index f433d822..6bbf6a75 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "chalk": "^4.1.2", "event-stream": "4.0.1", "form-data": "4.0.0", - "har-schema": "^2.0.0", + "har-validator-compiled": "^1.0.0", "stringify-object": "3.3.0", "yargs": "^17.4.0" } diff --git a/src/helpers/har-validator.ts b/src/helpers/har-validator.ts deleted file mode 100644 index 45d80271..00000000 --- a/src/helpers/har-validator.ts +++ /dev/null @@ -1,31 +0,0 @@ -import Ajv, { ErrorObject } from 'ajv'; -import { Request } from 'har-format'; -import * as schema from 'har-schema'; - -export class HARError extends Error { - name = 'HARError'; - message = 'validation failed'; - errors: ErrorObject[] = []; - constructor(errors: ErrorObject[]) { - super(); - this.errors = errors; - Error.captureStackTrace(this, this.constructor); - } -} - -const ajv = new Ajv({ - allErrors: true, -}); -ajv.addSchema(schema); - -export const validateHarRequest = (request: any): request is Request => { - const validate = ajv.getSchema('request.json'); - if (!validate) { - throw new Error('failed to find HAR request schema'); - } - const valid = validate(request); - if (!valid && validate.errors) { - throw new HARError(validate.errors); - } - return true; -}; diff --git a/src/httpsnippet.test.ts b/src/httpsnippet.test.ts index c945d6cd..5eab435f 100644 --- a/src/httpsnippet.test.ts +++ b/src/httpsnippet.test.ts @@ -19,7 +19,7 @@ describe('hTTPSnippet', () => { // @ts-expect-error intentionally incorrect const attempt = () => new HTTPSnippet({ ziltoid: 'the omniscient' }); - expect(attempt).toThrow('validation failed'); + expect(attempt).toThrow('Validation Failed'); }); it('should parse HAR file with multiple entries', () => { diff --git a/src/httpsnippet.ts b/src/httpsnippet.ts index 8cb5ccd7..0722c884 100644 --- a/src/httpsnippet.ts +++ b/src/httpsnippet.ts @@ -1,11 +1,11 @@ import { map as eventStreamMap } from 'event-stream'; import FormData from 'form-data'; import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format'; +import { validateRequest } from 'har-validator-compiled'; import { stringify as queryStringify } from 'querystring'; import { format as urlFormat, parse as urlParse, UrlWithParsedQuery } from 'url'; import { formDataIterator, isBlob } from './helpers/form-data'; -import { validateHarRequest } from './helpers/har-validator'; import { getHeaderName } from './helpers/headers'; import { ReducedHelperObject, reducer } from './helpers/reducer'; import { ClientId, TargetId, targets } from './targets/targets'; @@ -106,7 +106,7 @@ export class HTTPSnippet { }, }; - if (validateHarRequest(req)) { + if (validateRequest(req)) { this.requests.push(this.prepare(req)); } }); diff --git a/src/index.ts b/src/index.ts index 3a865a77..9b3bf454 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ export { CodeBuilder, CodeBuilderOptions, PostProcessor } from './helpers/code-builder'; export { EscapeOptions, escapeString } from './helpers/escape'; -export { HARError, validateHarRequest } from './helpers/har-validator'; export { getHeader, getHeaderName } from './helpers/headers'; export { AvailableTarget, availableTargets, extname } from './helpers/utils'; export { diff --git a/src/targets/har-schema.d.ts b/src/targets/har-schema.d.ts deleted file mode 100644 index 1df95659..00000000 --- a/src/targets/har-schema.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -declare module 'har-schema' { - const schema: object; - export default schema; -}