-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
336 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { json } from '@codemirror/lang-json'; | ||
import { FunctionComponent, useState } from 'react'; | ||
import BaseEditor from './base-editor'; | ||
import { getErrorPosition, initAjv, jsonLinter } from './json-utils'; | ||
|
||
/** | ||
* Contains a JSON object to be validated against a JSON schema | ||
* | ||
* @param props | ||
* @returns | ||
*/ | ||
const JsonSchemaContentEditor: FunctionComponent<{ | ||
value: string; | ||
schema: string; | ||
showValidMsg?: boolean; | ||
showErrors?: boolean; | ||
onValueChange?: (value: string) => void; | ||
}> = function (props) { | ||
const [hideGoToLine, setHideGoToLine] = useState<boolean>(false); | ||
const ajvInstance = initAjv(); | ||
|
||
return ( | ||
<BaseEditor | ||
lang='json' | ||
editorExtensions={[json(), jsonLinter]} | ||
onErrorChange={(currentValue: string, viewUpdate) => { | ||
try { | ||
JSON.parse(currentValue); | ||
|
||
const validate = ajvInstance.compile(JSON.parse(props.schema)); | ||
const isValid = validate(JSON.parse(currentValue)); | ||
|
||
if (!isValid) { | ||
setHideGoToLine(true); | ||
|
||
return { | ||
message: `Validation errors:\n${validate.errors | ||
.slice(0, 6) | ||
.map( | ||
(error) => | ||
`${error.instancePath ? error.instancePath.replace(/^\//, '') + ' ' : 'JSON '}${error.message}` | ||
) | ||
.join('\n')}` | ||
}; | ||
} | ||
|
||
return null; | ||
} catch (error) { | ||
setHideGoToLine(false); | ||
|
||
return { | ||
message: `JSON error: ${error.message}`, | ||
...getErrorPosition(error, viewUpdate.state.doc) | ||
}; | ||
} | ||
}} | ||
hideGoToLine={hideGoToLine} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default JsonSchemaContentEditor; |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { json } from '@codemirror/lang-json'; | ||
import { FunctionComponent } from 'react'; | ||
import BaseEditor from './base-editor'; | ||
import { getErrorPosition, initAjv, jsonLinter } from './json-utils'; | ||
|
||
/** | ||
* Contains a JSON schema | ||
* | ||
* @param props | ||
* @returns | ||
*/ | ||
const JsonSchemaEditor: FunctionComponent<{ | ||
value: string; | ||
showValidMsg?: boolean; | ||
showErrors?: boolean; | ||
onValueChange?: (value: string) => void; | ||
}> = function (props) { | ||
const ajvInstance = initAjv(); | ||
|
||
return ( | ||
<BaseEditor | ||
lang='json' | ||
editorExtensions={[json(), jsonLinter]} | ||
onErrorChange={(currentValue: string, viewUpdate) => { | ||
try { | ||
JSON.parse(currentValue); | ||
|
||
try { | ||
ajvInstance.compile(JSON.parse(currentValue)); | ||
} catch (error) { | ||
return { | ||
message: `Schema error: ${error.message.replace('strict mode: ', '')}`, | ||
...getErrorPosition(error, viewUpdate.state.doc) | ||
}; | ||
} | ||
return null; | ||
} catch (error) { | ||
return { | ||
message: `JSON error: ${error.message}`, | ||
...getErrorPosition(error, viewUpdate.state.doc) | ||
}; | ||
} | ||
}} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default JsonSchemaEditor; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { jsonParseLinter } from '@codemirror/lang-json'; | ||
import { linter } from '@codemirror/lint'; | ||
import { Text } from '@uiw/react-codemirror'; | ||
import Ajv from 'ajv'; | ||
import addFormats from 'ajv-formats'; | ||
|
||
export const jsonLinter = linter(jsonParseLinter(), { delay: 100 }); | ||
|
||
export function getErrorPosition( | ||
error: SyntaxError, | ||
doc: Text | ||
): { line?: number; column?: number; position?: number } { | ||
let match; | ||
if ((match = error.message.match(/at position (\d+)/))) | ||
return { position: Math.min(+match[1], doc.length) }; | ||
if ((match = error.message.match(/at line (\d+) column (\d+)/))) | ||
return { | ||
position: Math.min(doc.line(+match[1]).from + +match[2] - 1, doc.length) | ||
}; | ||
|
||
return { | ||
position: 1 | ||
}; | ||
} | ||
|
||
export function initAjv() { | ||
const ajv = new Ajv({ allErrors: true }); | ||
addFormats(ajv); | ||
return ajv; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.