Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field to easily check error in onChangeStatus #226

Closed
kloon15 opened this issue Feb 20, 2023 · 15 comments
Closed

Add field to easily check error in onChangeStatus #226

kloon15 opened this issue Feb 20, 2023 · 15 comments
Labels
enhancement New feature or request

Comments

@kloon15
Copy link
Contributor

kloon15 commented Feb 20, 2023

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
Copy link
Owner

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

@josdejong josdejong added the enhancement New feature or request label Feb 23, 2023
@kloon15
Copy link
Contributor Author

kloon15 commented Feb 23, 2023

That also works for me.

@josdejong
Copy link
Owner

👍

@josdejong
Copy link
Owner

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

@kloon15
Copy link
Contributor Author

kloon15 commented Feb 28, 2023

isContentValidationErrors returns true falsely for a valid document:

Errors: Object { validationErrors: [] }[create.tsx:100:36]
isContentValidationErrors: true [create.tsx:101:36]

@josdejong
Copy link
Owner

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
Copy link
Owner

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
Copy link
Contributor Author

kloon15 commented Feb 28, 2023

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
Copy link
Owner

👍 I'll implement that

@josdejong josdejong reopened this Feb 28, 2023
@josdejong
Copy link
Owner

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

@kloon15
Copy link
Contributor Author

kloon15 commented Mar 1, 2023

Sure ill test it.

@kloon15
Copy link
Contributor Author

kloon15 commented Mar 3, 2023

Works as expected, thank you!

@josdejong
Copy link
Owner

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

@kloon15
Copy link
Contributor Author

kloon15 commented Mar 3, 2023

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

@josdejong
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants