Skip to content

Commit

Permalink
feat: support for const auto-fix / suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed Oct 30, 2022
1 parent 89d262c commit a133c54
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ function pushErrors(
const errMessage =
`${error.message.charAt(0).toUpperCase()}` +
`${error.message.substring(1)}`;
/* Sub-path -OR- Root error? */
const expected = Array.isArray(error.params.allowedValues)
? `: \`${error.params.allowedValues.join('`, `')}\``
: '';

let expected = '';
if (Array.isArray(error.params.allowedValues)) {
expected = `: \`${error.params.allowedValues.join('`, `')}\``;
} else if (typeof error.params.allowedValue === 'string') {
expected = `: \`${error.params.allowedValue}\``;
}
const sPath = schemaRelPath ? ` • ${schemaRelPath}` : '';

reason = `${errMessage}${expected}${sPath}${error.schemaPath}`;
}

Expand Down Expand Up @@ -134,6 +138,11 @@ function pushErrors(

/* Auto-fix replacement suggestions for `enum` */
message.expected = error.params.allowedValues;
} else if (typeof error.params.allowedValue === 'string') {
note += `\nAllowed value: ${error.params.allowedValue}`;

/* Auto-fix replacement suggestion for `const` */
message.expected = [error.params.allowedValue];
}
if (typeof error.params.missingProperty === 'string') {
note += `\nMissing property: ${error.params.missingProperty}`;
Expand Down

0 comments on commit a133c54

Please sign in to comment.