I am confused about for which situation I am defining the properties in my json schemas.
Assume I have an item product
for which I am trying to define a schema. In my database the products
table has id
, brand_id
, name
, item_number
and description
. All except description
are required fields. The id
is autogenerated by the database and the brand_id
is set upon creation automatically by the api based on the user creating.
This means I can POST /api/products
using only the following data:
{
"product": {
"name": "Product Name",
"item_number": "item001"
}
}
However, how should I now define the product schema? Should I include the properties id
and brand_id
? If so, should I label them as required, even though they are set automatically?
This is what I came up with:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net/products",
"type": "object",
"properties": {
"item_number": {
"id": "http://jsonschema.net/products/item_number",
"type": "string"
},
"name": {
"id": "http://jsonschema.net/products/name",
"type": "string"
},
"description": {
"id": "http://jsonschema.net/products/description",
"type": "string",
"default": "null"
}
},
"required": [
"item_number",
"name"
]
}