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 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 1, 2024
1 parent 0602b02 commit 0253d82
Show file tree
Hide file tree
Showing 22 changed files with 785 additions and 14 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@
"vitest": "^1.2.2"
},
"dependencies": {
"@babel/code-frame": "^7.23.5",
"@babel/runtime": "^7.23.9",
"@humanwhocodes/momoa": "^3.0.0",
"@readme/better-ajv-errors": "^1.6.0",
"@types/node": "^20.11.10",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^2.1.1",
"chalk": "^5.3.0",
"glob": "^10.3.10",
"js-yaml": "^4.1.0",
"json-to-ast": "^2.1.0",
"jsonpointer": "^5.0.1",
"leven": "^4.0.0",
"openapi-types": "^12.1.3",
"vite": "^5.0.12",
"vite-node": "^1.2.2"
Expand Down
173 changes: 170 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import Ajv04 from 'ajv-draft-04'
import addFormats from 'ajv-formats'
import Ajv2020 from 'ajv/dist/2020'
import { JSON_SCHEMA, load } from 'js-yaml'
import type { AjvOptions, Specification, ValidationResult } from '../types'
import type {
AjvOptions,
Specification,
ValidationResult,
ValidateOptions,
} from '../types'
import { checkRefs, replaceRefs } from './resolve'
import betterAjvErrors from '../utils/betterAjvErrors'

const supportedVersions = new Set(['2.0', '3.0', '3.1'])

Expand Down Expand Up @@ -141,7 +147,10 @@ export class Validator {
this.externalRefs[newUri] = spec
}

async validate(data: string | object): Promise<ValidationResult> {
async validate(
data: string | object,
options?: ValidateOptions,
): Promise<ValidationResult> {
const specification = await getSpecFromData(data)

this.specification = specification
Expand Down Expand Up @@ -185,7 +194,22 @@ export class Validator {
}

if (validateSchema.errors) {
result.errors = validateSchema.errors
if (typeof validateSchema.errors === 'string') {
result.errors = validateSchema.errors
} else {
result.errors = betterAjvErrors(
schemaResult,
{},
validateSchema.errors,
{
format: options?.format ?? 'js',
indent: options?.indent ?? 2,
colorize: false,
},
)
}

console.log(result.errors)
}

return result
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export type ValidationResult = {
errors?: string | ErrorObject[]
}

export type ValidateOptions = {
format?: 'js' | 'cli'
indent?: number
}

export type ParseResult = OpenAPI.Document

export type EmptyObject = Record<string, never>
Expand Down
Loading

0 comments on commit 0253d82

Please sign in to comment.