forked from atlassian/better-ajv-errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make better-ajv-errors browser compatible
- Loading branch information
Showing
15 changed files
with
1,648 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
.vscode | ||
.env | ||
package-lock.json | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
src/validation-errors/__tests__/__snapshots__/enum.js.snap
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
src/validation-errors/__tests__/__snapshots__/required.js.snap
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,56 @@ | ||
import chalk from 'chalk'; | ||
import BaseValidationError from './base'; | ||
import leven from 'leven'; | ||
import pointer from 'jsonpointer'; | ||
|
||
export default class AdditionalPropValidationError extends BaseValidationError { | ||
constructor(...args) { | ||
super(...args); | ||
this.options.isIdentifierLocation = true; | ||
} | ||
|
||
print() { | ||
const { message, dataPath, params } = this.options; | ||
const output = [chalk`{red {bold ADDTIONAL PROPERTY} ${message}}\n`]; | ||
|
||
return output.concat( | ||
this.getCodeFrame( | ||
chalk`😲 {magentaBright ${params.additionalProperty}} is not expected to be here!`, | ||
`${dataPath}/${params.additionalProperty}` | ||
) | ||
); | ||
} | ||
|
||
getError() { | ||
const { params, dataPath } = this.options; | ||
|
||
return { | ||
const bestMatch = this.findBestMatch(); | ||
//console.log(this.options) | ||
const output = { | ||
...this.getLocation(`${dataPath}/${params.additionalProperty}`), | ||
error: `${this.getDecoratedPath(dataPath)} Property ${ | ||
params.additionalProperty | ||
} is not expected to be here`, | ||
path: dataPath, | ||
suggestion: `Allowed values are: ${Object.keys( | ||
this.schema.properties | ||
).join(', ')}`, | ||
}; | ||
|
||
if (bestMatch !== null) { | ||
output.suggestion = `Did you mean '${bestMatch}'? ` + output.suggestion; | ||
} | ||
return output; | ||
} | ||
|
||
findBestMatch() { | ||
const { params } = this.options; | ||
|
||
const allowedValues = Object.keys(this.schema.properties); | ||
const currentValue = params.additionalProperty; | ||
|
||
if (!currentValue) { | ||
return null; | ||
} | ||
|
||
const bestMatch = allowedValues | ||
.map(value => ({ | ||
value, | ||
weight: leven(value, currentValue.toString()), | ||
})) | ||
.sort((x, y) => | ||
x.weight > y.weight ? 1 : x.weight < y.weight ? -1 : 0 | ||
)[0]; | ||
|
||
return allowedValues.length === 1 || | ||
bestMatch.weight < bestMatch.value.length | ||
? bestMatch.value | ||
: null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.