jschon icon indicating copy to clipboard operation
jschon copied to clipboard

Validate class object with jsonschema

Open jugaadi opened this issue 2 years ago • 4 comments

Will it be a good idea to pursue class object validation with jsonschema? Can we maintain a protocol instead of the current JSON class ?

jugaadi avatar Mar 01 '23 03:03 jugaadi

@jugaadi I'm (obviously) not the owner of this project, but I'm confused about what you are asking. What do you mean by "class object validation"? JSON Schema works on its own JSON-derived data model which doesn't have a concept of class as in OO programming.

jschon's JSON class does a lot of important work, particularly by enabling upward traversals, and removing it would require a drastic re-architecting of the package.

handrews avatar Mar 04 '23 00:03 handrews

@jugaadi I'm (obviously) not the owner of this project, but I'm confused about what you are asking. What do you mean by "class object validation"? JSON Schema works on its own JSON-derived data model which doesn't have a concept of class as in OO programming.

Yes.. I was wondering whether how I can use JSON Schema to define constraints for dataclass/pydantic class object.

jschon's JSON class does a lot of important work, particularly by enabling upward traversals, and removing it would require a drastic re-architecting of the package.

Completely agree. I wanted to know how to make a dataclass/pydantic equivalent of the jschon JSON class.

jugaadi avatar Mar 04 '23 04:03 jugaadi

@jugaadi oh, I see. My Python knowledge is a little dated and I had not been aware of the dataclass standard module, although I've heard of Pydantic. It looks like Pydantic will generate JSON Schemas from their version of the module, but it looks like thy're using draft-04/OpenAPI 3.0.

So when you say "maintain a protocol" you mean some sort of interface that both jschon's JSON class and dataclasses could implement? Alternatively, you could add a jschon.json.JSON constructor that took a dataclass in lieu of regular JSON data.... hmmm.... interesting. I'm not sure I know what the ideal way to do this would be, but I think I understand your point now.

Are you trying to both generate a schema from a dataclass (as Pydantic does) and treate dataclass instances as schema instances for validatin/annotation purposes?

handrews avatar Mar 04 '23 17:03 handrews

@handrews Thanks for your inputs.

@jugaadi oh, I see. My Python knowledge is a little dated and I had not been aware of the dataclass standard module, although I've heard of Pydantic. It looks like Pydantic will generate JSON Schemas from their version of the module, but it looks like thy're using draft-04/OpenAPI 3.0.

dataclass/pydantic is just an example. It can be a regular class instance too.

So when you say "maintain a protocol" you mean some sort of interface that both jschon's JSON class and dataclasses could implement? Alternatively, you could add a jschon.json.JSON constructor that took a dataclass in lieu of regular JSON data.... hmmm.... interesting. I'm not sure I know what the ideal way to do this would be, but I think I understand your point now.

Yes. Some form of extensibility would help a lot.

Are you trying to both generate a schema from a dataclass (as Pydantic does) and treate dataclass instances as schema instances for validatin/annotation purposes?

We're basically marshalling the pydantic/dataclass/regular object into another schema-defined json. Therefore, the source schema doesnt matter much.

jugaadi avatar Mar 06 '23 02:03 jugaadi