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

Feature request: Selection of object properties/keys with values of the correct type. #141

Open
DameNianch opened this issue Dec 20, 2024 · 4 comments

Comments

@DameNianch
Copy link

Describe the Difficulty
When entering a key to add, the default value is maybe null, so it is rejected by validate.
I'm imitating the onUpdate validation in the demo source code. But I can't add a value because it's rejected by that validation.

Describe the Request
When entering a key to add, it would be nice if the appropriate value was already entered.

Online demo
I'm imitating the jsonSchemaValidation in the sample demo that was released to the public. Since the name doesn't exist, I'll add it, but I'll get a validation error.
https://codesandbox.io/p/sandbox/json-edit-react-demo-forked-2d6xlx?file=%2Fsrc%2FApp.js%3A9%2C13&workspaceId=ws_CTwx66TnRUb2vkBSHCXygH

@DameNianch
Copy link
Author

This request appears to be related to this one:
#95

@CarlosNZ
Copy link
Owner

Okay, couple of things:

  • The schema you used in the example is actually the schema for the Custom Nodes demo, not the "Tony Stark" one, so nothing in that example data is actually valid. The correct schema for that data is this one
  • You can change the default value to a string with the defaultValue prop to make sure its appropriate. I just set it to a string to get it work for this case, but you can set a more complex conditional default by passing a function to that prop.

So here is a modification with the correct schema a string default and it's working as expected when adding the name key:

https://codesandbox.io/p/sandbox/json-edit-react-demo-forked-wqsmkc?file=%2Fsrc%2FApp.js%3A47%2C26

Hope that helps :)

@DameNianch
Copy link
Author

The schema you used in the example is actually the schema for the Custom Nodes demo, not the "Tony Stark" one, so nothing in that example data is actually valid. The correct schema for that data is this one

Thank you for your kind reply even for a silly mistake.
Since Custom Nodes is set to localhost, it would be helpful to have a public link to help me understand.

You can change the default value to a string with the defaultValue prop to make sure its appropriate. I just set it to a string to get it work for this case, but you can set a more complex conditional default by passing a function to that prop.

My intuition is that when I provide a JSON Schema, I have to provide a defaultValue (=DefaultValueFilterFunction) at the same time. It feels natural that the correct type is applied to defaultValue by jsonSchema.
It depends on the philosophy of json-edit-react, but for me, it is more convenient to receive jsonSchema.

@CarlosNZ
Copy link
Owner

Thank you for your kind reply even for a silly mistake. Since Custom Nodes is set to localhost, it would be helpful to have a public link to help me understand.

No problem. Sorry, I posted my local link rather than the public demo: https://carlosnz.github.io/json-edit-react/?data=customNodes

My intuition is that when I provide a JSON Schema, I have to provide a defaultValue (=DefaultValueFilterFunction) at the same time. It feels natural that the correct type is applied to defaultValue by jsonSchema. It depends on the philosophy of json-edit-react, but for me, it is more convenient to receive jsonSchema.

Yes, it would be good to have default values generated automatically, but adding this into the core package is beyond the scope of what this tool should do for now. We can do JSON schema validation, but the actual validation is done by an external tool, so I imagine something similar could be done for this. It would involve fairly complicated JSON schema parsing, so I wouldn't want to include it in the main package, but if someone wanted to create one, it could be a good supplement. You'd need to write a function to input a JSON schema and output a "DefaultValueFilterFunction" -- definitely a non-trivial task. I might have a crack at it one day, but it'd be great if someone wanted to contribute one.

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

No branches or pull requests

2 participants