Closed kloon15 closed 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
That also works for me.
👍
The just published v0.14.10
now exports isContentParseError
and isContentValidationErrors
.
isContentValidationErrors
returns true
falsely for a valid document:
Errors: Object { validationErrors: [] }[create.tsx:100:36]
isContentValidationErrors: true [create.tsx:101:36]
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'
}
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
.
Thinking about it, I think the editor should not return an empty
contentErrors : { validationErrors: [] }
in case of a valid document, butnull
in that case. Then your check simplifies tocontentErrors === null
.
Yea that would be the optimal solution.
👍 I'll implement that
Published now in v0.15.0
. Can you give it a try @kloon15?
Sure ill test it.
Works as expected, thank you!
Thanks for the feedback and for helping make the library a bit better 😄
My pleasure, now i just need to figure out how to deal with Date objects in it.
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.
Right now if i need to check if the JSON is valid i have to write a long ass sentence:
I would like to have a field to easily check for error and its type:
Thank you.