json-schema-org / json-schema-spec

The JSON Schema specification
http://json-schema.org/
Other
3.43k stars 251 forks source link

To validate CRUD of table information with a single Json-Schema, we propose an attribute for PrimaryKey. #1495

Open Yuutakasan opened 3 months ago

Yuutakasan commented 3 months ago

I am currently defining an API in OpenAPI Spec, and it is great that Json-Schema can validate data structures, but I am having trouble separating Json-Schema from Json-Schema for INSERT, SELECT, UPDATE, and DELETE, because the handling of required items changes. However, I am having trouble separating Json-Schema for INSERT, SELECT, UPDATE, and DELETE because the handling of required fields changes.

I propose that the PrimaryKey attribute be set so that the same Json-schema can be used to validate each CRUD operation and the corresponding HTTP Method request.

We would like the PrimaryKey attribute to be ignored for INSERT and evaluated as required for UPDATE, PATHCH, and DELETE operations when the validator is told which CRUD operation is being processed.

It is painful that in the case of Entity definitions in ORM, one definition can manage all CRUDs, but Json-Schema does not.

Again, if anyone knows of a good solution, please let me know.

Era-cell commented 3 months ago

Hi, @Yuutakasan can you provide an example, there isn't a reserved keyword for primary key but can help you with a solution: As per my present understanding you can use

 if : {
   properties: name:{enum:[ "patch", "update", "delete"]},
}then{
  properties: primarykey: { your conastraints on primary key goes here}
  required: ["primarykey"]
}else{
  not {required: ["primarykey"]}
}

you can use else if you do not want "primarykey" attribute at any case during insert opearation

Yuutakasan commented 3 months ago

@Era-cell Oh Thanx! I see. My editor just doesn't support the if else syntax, so my lack of understanding was the problem. Then it's not a problem with the Json-shema specification.

Thanks for the very clear explanation.

This reserved word itself should be there if possible.

Era-cell commented 3 months ago

@Yuutakasan I didn't get it do you want another way of implementing the same, or if you got the solution I guess you can close the issue. 😁 And I guess authors are working on primary key, looks like its been asked by many people: "primarykey"

gregsdennis commented 2 weeks ago

@Yuutakasan please confirm, but I think this is a duplicate of #1488.