josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
820 stars 108 forks source link

Add field to easily check error in onChangeStatus #226

Closed kloon15 closed 1 year ago

kloon15 commented 1 year ago

Right now if i need to check if the JSON is valid i have to write a long ass sentence:

const valid = ((status.contentErrors as ContentParseError).parseError ||
                            (status.contentErrors as ContentValidationErrors).validationErrors?.length)
                            ? false : true

I would like to have a field to easily check for error and its type:

errorType: "validation" | "parse" | "none"

Thank you.

josdejong commented 1 year ago

We could expose the two internal functions (typeguards) isContentParseError and isContentValidationErrors, would that address your issue?

Code: https://github.com/josdejong/svelte-jsoneditor/blob/main/src/lib/typeguards.ts#L72-L86

kloon15 commented 1 year ago

That also works for me.

josdejong commented 1 year ago

👍

josdejong commented 1 year ago

The just published v0.14.10 now exports isContentParseError and isContentValidationErrors.

kloon15 commented 1 year ago

isContentValidationErrors returns true falsely for a valid document:

Errors: Object { validationErrors: [] }[create.tsx:100:36]
isContentValidationErrors: true [create.tsx:101:36]
josdejong commented 1 year ago

These two functions are typeguards, they only check the type.

Depending on your exact needs you can do something like:

function getContentErrorsType(contentErrors: ContentErrors) : 'parse' | 'validation' | 'none'  {
  return isContentParseError(contentErrors)
    ? 'parse'
    : isContentValidationErrors(contentErrors) && contentErrors.validationErrors.length > 0
    ? 'validation'
    : 'none'
}
josdejong commented 1 year ago

Thinking about it, I think the editor should not return an empty contentErrors : { validationErrors: [] } in case of a valid document, but null in that case. Then your check simplifies to contentErrors === null.

kloon15 commented 1 year ago

Thinking about it, I think the editor should not return an empty contentErrors : { validationErrors: [] } in case of a valid document, but null in that case. Then your check simplifies to contentErrors === null.

Yea that would be the optimal solution.

josdejong commented 1 year ago

👍 I'll implement that

josdejong commented 1 year ago

Published now in v0.15.0. Can you give it a try @kloon15?

kloon15 commented 1 year ago

Sure ill test it.

kloon15 commented 1 year ago

Works as expected, thank you!

josdejong commented 1 year ago

Thanks for the feedback and for helping make the library a bit better 😄

kloon15 commented 1 year ago

My pleasure, now i just need to figure out how to deal with Date objects in it.

josdejong commented 1 year ago

now i just need to figure out how to deal with Date objects in it

You can configure your own JSON parser with the parser option, so you can provide a JSON parser where you revive and stringify Dates.