Skip to content

Commit 903e8e7

Browse files
committed
add comments
1 parent 7661dd9 commit 903e8e7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/schema-parser/asyncapi-schema-parser.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function validate(input: ValidateSchemaInput<unknown, unknown>): Promise<S
2323
ajv.compile(schema);
2424
} catch (error: any) {
2525
if (error! instanceof Error) {
26-
errors = AjvToSpectralErrors(error);
26+
errors = ajvToSpectralErrors(error);
2727
} else {
2828
// Unknown and unexpected error
2929
throw error;
@@ -33,31 +33,37 @@ async function validate(input: ValidateSchemaInput<unknown, unknown>): Promise<S
3333
return errors;
3434
}
3535

36-
function AjvToSpectralErrors(error: Error): SchemaValidateError[] {
36+
function ajvToSpectralErrors(error: Error): SchemaValidateError[] {
3737
let errors: SchemaValidateError[] = [];
3838
let errorMessage = error.message;
3939

40-
// Validation errors
40+
// Validation errors.
41+
// See related AJV function where the error message is generated:
42+
// https://github.com/ajv-validator/ajv/blob/99e884dc4bbb828cf47771b7bbdb14f23193b0b1/lib/core.ts#L501-L522
4143
const validationErrorPrefix = "schema is invalid: ";
4244
if (error.message.startsWith(validationErrorPrefix)) {
4345
// remove prefix
4446
errorMessage = errorMessage.substring(validationErrorPrefix.length);
4547

4648
// message can contain multiple validation errors separated by ',' (comma)
4749
errorMessage.split(", ").forEach((message: string) => {
48-
const path = message.slice(0, message.indexOf(" "));
49-
const error = message.slice(message.indexOf(" ") + 1);
50+
const splitIndex = message.indexOf(" ");
51+
const path = message.slice(0, splitIndex);
52+
const error = message.slice(splitIndex + 1);
53+
5054
const resultErr: SchemaValidateError = {
5155
message: error,
5256
path: path.split("/")
5357
};
58+
5459
errors.push(resultErr);
5560
});
5661
} else {
5762
// Not a validation error
5863
const resultErr: SchemaValidateError = {
5964
message: error.message,
6065
};
66+
6167
errors.push(resultErr);
6268
}
6369

0 commit comments

Comments
 (0)