json-viewer icon indicating copy to clipboard operation
json-viewer copied to clipboard

Error Validation

Open kozlovskk opened this issue 1 year ago • 9 comments

Hello, is there a way to implement custom error messages/validation for fields that are empty or expect a number, but string is passed for example? In the READ.me there is not mention, so just in case, wanted to ask here.

kozlovskk avatar Jun 07 '24 09:06 kozlovskk

This is not supported at the moment. The JSON viewer itself supports passing a custom Editor for users who want to add new features or limitations on the input. See https://viewer.textea.io/how-to/data-types#editor

However, editor props do not contain path to retrieve path-specific validation rules. https://github.com/TexteaInc/json-viewer/blob/0a1bcac5ff1e3b09a0556f0ddb5f650ecb2a6690/src/type.ts#L65-L70

Based on this, we can add path to the props to achieve what you want. Let me check the code later and get back to you, and possibly write an example/demo in the document. 🙏

pionxzh avatar Jun 07 '24 09:06 pionxzh

Thank you for your response, will test this out :)

kozlovskk avatar Jun 07 '24 11:06 kozlovskk

I will prepare a demo after #498 is merged.

pionxzh avatar Aug 13 '24 18:08 pionxzh

Open in StackBlitz

image

Please check this demo with the input validation. I provided two implementations with different strategies. But this example is not perfect because the library does not provide a way to fully control the editing process. Users can still click the ✓ to submit changes.

I would suggest you use the commented way I left in the demo to disallow any invalid input.

pionxzh avatar Aug 27 '24 19:08 pionxzh

Reopen since the library might need to have a way to do the checking before submitting the changes.

pionxzh avatar Aug 27 '24 19:08 pionxzh

Would it be an idea to have some integration with JSON schemas?

ianldgs avatar Apr 02 '25 09:04 ianldgs

@ianldgs I would expect it to be implemented in userland.

pionxzh avatar Apr 05 '25 10:04 pionxzh

Yeah, just validation could make sense to do in user land. But facilitating editing, e.g. auto suggesting keys/values, could be a generic thing. Anyway, I had to move to Monaco editor for that, given I couldn't work out how to allow users to edit object keys.

ianldgs avatar Apr 05 '25 11:04 ianldgs

Understandable. I would position this library as a JSON "viewer" with light editing abilities. Monaco would be more appropriate in some cases. 🙏

pionxzh avatar Apr 05 '25 12:04 pionxzh