Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 1, 2024
1 parent 0253d82 commit ce02bac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 47 deletions.
109 changes: 65 additions & 44 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ const ERRORS = {

const inlinedRefs = 'x-inlined-refs'

function makeErrorArray(error: string) {
return [
{
start: {
line: 1,
column: 1,
offset: 0,
},
error,
path: '',
},
]
}

function getOpenApiVersion(specification: Specification) {
for (const version of supportedVersions) {
const specificationType = version === '2.0' ? 'swagger' : 'openapi'
Expand Down Expand Up @@ -151,68 +165,75 @@ export class Validator {
data: string | object,
options?: ValidateOptions,
): Promise<ValidationResult> {
const specification = await getSpecFromData(data)
try {
const specification = await getSpecFromData(data)

this.specification = specification
this.specification = specification

if (specification === undefined || specification === null) {
return {
valid: false,
errors: ERRORS.EMPTY_OR_INVALID,
if (specification === undefined || specification === null) {
return {
valid: false,
errors: makeErrorArray(ERRORS.EMPTY_OR_INVALID),
}
}
}

if (Object.keys(this.externalRefs).length > 0) {
specification[inlinedRefs] = this.externalRefs
}
if (Object.keys(this.externalRefs).length > 0) {
specification[inlinedRefs] = this.externalRefs
}

const { version, specificationType, specificationVersion } =
getOpenApiVersion(specification)
this.version = version
this.specificationVersion = specificationVersion
this.specificationType = specificationType
const { version, specificationType, specificationVersion } =
getOpenApiVersion(specification)

if (!version) {
return {
valid: false,
errors: ERRORS.OPENAPI_VERSION_NOT_SUPPORTED,
this.version = version
this.specificationVersion = specificationVersion
this.specificationType = specificationType

if (!version) {
return {
valid: false,
errors: makeErrorArray(ERRORS.OPENAPI_VERSION_NOT_SUPPORTED),
}
}
}

const validateSchema = await this.getAjvValidator(version)
const validateSchema = await this.getAjvValidator(version)

// Check if the specification matches the JSON schema
const schemaResult = validateSchema(specification)
// Check if the specification matches the JSON schema
const schemaResult = validateSchema(specification)

// Check if the references are valid as those can’t be validated bu JSON schema
if (schemaResult) {
return checkRefs(specification)
}
// Check if the references are valid as those can’t be validated bu JSON schema
if (schemaResult) {
return checkRefs(specification)
}

const result: ValidationResult = {
valid: schemaResult,
}
const result: ValidationResult = {
valid: schemaResult,
}

if (validateSchema.errors) {
let errors = []

if (validateSchema.errors) {
if (typeof validateSchema.errors === 'string') {
result.errors = validateSchema.errors
} else {
result.errors = betterAjvErrors(
schemaResult,
{},
validateSchema.errors,
{
if (typeof validateSchema.errors === 'string') {
errors = makeErrorArray(validateSchema.errors)
} else {
errors = validateSchema.errors
}

if (errors.length > 0) {
result.errors = betterAjvErrors(schemaResult, {}, errors, {
format: options?.format ?? 'js',
indent: options?.indent ?? 2,
colorize: false,
},
)
})
}
}

console.log(result.errors)
return result
} catch (error) {
return {
valid: false,
errors: makeErrorArray(error.message ?? error),
}
}

return result
}

async getAjvValidator(version: string) {
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ export type { ErrorObject } from 'ajv'

export type ValidationResult = {
valid: boolean
errors?: string | ErrorObject[]
errors?: {
start: {
line: number
column: number
offset: number
}
error: string
path: string
}[]
}

export type ValidateOptions = {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export async function parse(value: string): Promise<ParseResult> {
const result = await validator.validate(value)

if (!result.valid) {
// return result.errors
throw new Error(`Invalid Schema: ${result.errors}`)
throw new Error(JSON.stringify(result.errors, null, 2))
}

return validator.resolveRefs() as ParseResult
Expand Down

0 comments on commit ce02bac

Please sign in to comment.