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

Commit

Permalink
refactor: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 1, 2024
1 parent ce02bac commit 354447c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JSON_SCHEMA, load } from 'js-yaml'
import type {
AjvOptions,
Specification,
ValidationResult,
ValidateResult,
ValidateOptions,
} from '../types'
import { checkRefs, replaceRefs } from './resolve'
Expand Down Expand Up @@ -164,7 +164,7 @@ export class Validator {
async validate(
data: string | object,
options?: ValidateOptions,
): Promise<ValidationResult> {
): Promise<ValidateResult> {
try {
const specification = await getSpecFromData(data)

Expand Down Expand Up @@ -205,7 +205,7 @@ export class Validator {
return checkRefs(specification)
}

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

Expand Down
32 changes: 17 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { type ErrorObject } from 'ajv'

export type { ErrorObject } from 'ajv'

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

export type ErrorObject = {
start: {
line: number
column: number
offset: number
}
error: string
path: string
}

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

export type ParseResult = OpenAPI.Document
export type ParseResult = {
valid: boolean
document?: OpenAPI.Document
errors?: ErrorObject[]
}

export type EmptyObject = Record<string, never>

Expand Down
21 changes: 18 additions & 3 deletions src/utils/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,27 @@ describe('parse', async () => {
"paths": {}
}`)

expect(result.info.title).toBe('Hello World')
expect(result.document.info.title).toBe('Hello World')
})

it('throws an error when the schema is invalid', async () => {
await expect(() => parse('pineapples')).rejects.toThrowError(
'Invalid Schema: Cannot find JSON, YAML or filename in data',
const result = await parse('pineapples')

expect(result.valid).toBe(false)
expect(result.errors).toHaveLength(1)
expect(result.errors[0].error).toBe(
'Cannot find JSON, YAML or filename in data',
)
})

it('works with YAML', async () => {
const result = await parse(`openapi: 3.1.0
info:
title: Hello World
version: 1.0.0
paths: {}
`)

expect(result.document.info.title).toBe('Hello World')
})
})
12 changes: 9 additions & 3 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Validator } from '../lib'
import { ParseResult } from '../types'
import type { ParseResult, OpenAPI } from '../types'

/**
* Validates an OpenAPI schema and resolves all references.
Expand All @@ -10,8 +10,14 @@ export async function parse(value: string): Promise<ParseResult> {
const result = await validator.validate(value)

if (!result.valid) {
throw new Error(JSON.stringify(result.errors, null, 2))
return {
valid: false,
errors: result.errors,
}
}

return validator.resolveRefs() as ParseResult
return {
valid: true,
document: validator.resolveRefs() as OpenAPI.Document,
}
}
12 changes: 11 additions & 1 deletion src/utils/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ describe('validate', async () => {
const result = await validate('')

expect(result.valid).toBe(false)
expect(result.errors).toBe('Cannot find JSON, YAML or filename in data')
expect(result.errors).toMatchObject([
{
error: 'Cannot find JSON, YAML or filename in data',
path: '',
start: {
column: 1,
line: 1,
offset: 0,
},
},
])
})

it('returns errors for an invalid schema', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Validator } from '../lib'
import type { ValidateOptions, ValidationResult } from '../types'
import type { ValidateOptions, ValidateResult } from '../types'

/**
* Validates an OpenAPI schema.
*/
export async function validate(
value: string,
options?: ValidateOptions,
): Promise<ValidationResult> {
): Promise<ValidateResult> {
const validator = new Validator()
const result = await validator.validate(value, options)

Expand Down
2 changes: 1 addition & 1 deletion tests/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ describe.sequential('files:parse', async () => {
const content = fs.readFileSync(file, 'utf-8')
const result = await parse(content)

expect(result.info.title).not.toBe(undefined)
expect(result.document.info.title).not.toBe(undefined)
})
})

0 comments on commit 354447c

Please sign in to comment.