Skip to content

Commit 91c7105

Browse files
committed
feat: add support to validate JSON Schemas
1 parent 5edb3ab commit 91c7105

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { Config, APIHealthResponse, CommandResult } from './types.js'
33
import { readFileSync } from 'fs'
44
import { fileURLToPath } from 'url'
55
import { dirname, join } from 'path'
6+
import Ajv from 'ajv'
7+
import addFormats from 'ajv-formats'
8+
9+
// Use type assertions to bypass TypeScript errors
10+
// @TODO: Remove type assertions when ajv-formats is updated
11+
const ajv = new (Ajv as any)()
12+
;(addFormats as any)(ajv)
613

714
const __filename = fileURLToPath(import.meta.url)
815
const __dirname = dirname(__filename)
@@ -43,3 +50,18 @@ export const handleCommandResult = (result: CommandResult) => {
4350
process.exit(1)
4451
}
4552
}
53+
54+
export const validateData = (data: any, schema: any) => {
55+
const validate = ajv.compile(schema)
56+
const valid = validate(data)
57+
if (!valid) {
58+
return {
59+
success: false,
60+
messages: validate.errors?.map((error: any) => error.message) || []
61+
}
62+
}
63+
return {
64+
success: true,
65+
messages: []
66+
}
67+
}

0 commit comments

Comments
 (0)