diff --git a/servlets/json-schema/src/main.ts b/servlets/json-schema/src/main.ts index 4788702..4455d9b 100644 --- a/servlets/json-schema/src/main.ts +++ b/servlets/json-schema/src/main.ts @@ -35,27 +35,39 @@ function validate(input: CallToolRequest): CallToolResult { throw new Error("Missing required argument: document"); } - const ajv = new Ajv(); - const validate = ajv.compile(schema); - const valid = validate(document); + try { + const ajv = new Ajv(); + const validate = ajv.compile(schema); + const valid = validate(document); - if (!valid) { - return { - content: [ - { - type: ContentType.Text, - mimeType: "application/json", - text: JSON.stringify({ valid: false, errors: validate.errors}, null, 2) - } - ] + if (!valid) { + return { + content: [ + { + type: ContentType.Text, + mimeType: "application/json", + text: JSON.stringify({ valid: false, errors: validate.errors }, null, 2) + } + ] + } + } else { + return { + content: [ + { + type: ContentType.Text, + mimeType: "application/json", + text: JSON.stringify({ valid: true }, null, 2) + } + ] + } } - } else { + } catch (e) { return { content: [ { type: ContentType.Text, mimeType: "application/json", - text: JSON.stringify({ valid: true }, null, 2) + text: JSON.stringify({ valid: false, errors: [{ message: (e as any).message }] }, null, 2) } ] } diff --git a/servlets/json-schema/test.sh b/servlets/json-schema/test.sh index 2b619ef..0bfc37d 100644 --- a/servlets/json-schema/test.sh +++ b/servlets/json-schema/test.sh @@ -1 +1 @@ -xtp plugin call ./dist/plugin.wasm --wasi --input '{ "params": { "name": "validate", "arguments": { "schema": {"$schema":"http://json-schema.org/draft-07/schema#","title":"Person","type":"object","properties":{"name":{"type":"string","description":"The person'\''s full name"},"age":{"type":"integer","minimum":0,"description":"The person'\''s age in years"},"isStudent":{"type":"boolean","description":"Whether the person is a student"}},"required":["name","age"]}, "document": {"name":"John Doe","age":25,"isStudent":true} } } }' call \ No newline at end of file +xtp plugin call ./dist/plugin.wasm --wasi --input '{ "params": { "name": "validate", "arguments": { "schema": {"$schema":"http://json-schema.org/draft-07/schema#","title":"Person","type":"object","properties":{"name":{"type":"string","description":"The person'\''s full name"},"age":{"type":"integer","minimum":0,"description":"The person'\''s age in years"},"isStudent":{"type":"boolean","description":"Whether the person is a student"}},"required":["name","age"]}, "document": {"namae":"John Doe","age":25,"isStudent":true} } } }' call \ No newline at end of file