Skip to content

Commit

Permalink
error suggestions for json schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Oct 3, 2024
1 parent ecb3ca5 commit 97eda04
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions catalog/app/components/FileEditor/TextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const useEditorTextStyles = M.makeStyles((t) => ({
},
helperText: {
marginTop: t.spacing(0.5),
whiteSpace: 'pre-wrap', // TODO: use JsonValidationErrors
},
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface SingleErrorProps {
error: Err
}

// TODO: use more humble layout similar to TextField#helperText
function SingleError({ className, error }: SingleErrorProps) {
const classes = useSingleErrorStyles()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ErrorObject } from 'ajv'
import * as React from 'react'
import * as FF from 'final-form'
import * as RF from 'react-final-form'
Expand Down Expand Up @@ -47,7 +48,8 @@ export const validateTable: FF.FieldValidator<string> = (inputStr?: string) => {
const validator = makeSchemaValidator(tabulatorTableSchema)
const errors = validator(data)
if (errors.length) {
return new JsonInvalidAgainstSchema({ errors }).message
if (errors[0] instanceof Error) return errors[0].message
return new JsonInvalidAgainstSchema({ errors: errors as ErrorObject[] }).message
}
return undefined
} catch (error) {
Expand Down
17 changes: 12 additions & 5 deletions catalog/app/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ErrorObject } from 'ajv'
/**
* Extensible error class.
*/
Expand Down Expand Up @@ -42,7 +43,7 @@ export class ErrorDisplay extends BaseError {
}

export interface JsonInvalidAgainstSchemaProps {
errors: { instancePath?: string; message?: string }[]
errors: ErrorObject[]
}

export class JsonInvalidAgainstSchema extends BaseError {
Expand All @@ -51,10 +52,16 @@ export class JsonInvalidAgainstSchema extends BaseError {
constructor(props: JsonInvalidAgainstSchemaProps) {
super(
props.errors
.map(({ instancePath, message }) =>
instancePath ? `${instancePath} ${message}` : message,
)
.join(', '),
.map(({ instancePath, message, params, keyword }) => {
switch (keyword) {
case 'const':
return `${instancePath}: ${message}: ${params.allowedValue}`
case 'enum':
return `${instancePath}: ${message}: ${params.allowedValues.join(', ')}`
}
return `${instancePath}: ${message}`
})
.join('.\n'),
props,
)
}
Expand Down

0 comments on commit 97eda04

Please sign in to comment.