Manage using API¶
Schemas can be programmatically managed. Prerequisite is to have an API key with permissions that allow schema module operations. The dashboard (API key wizard) can be used to quickly create such API key as explained in API key section Manage using dashboard. Such API key might have following permissions:
Path | Methods | ACL |
---|---|---|
/schemas/core\.schema/[0-9a-fA-F]{24} | POST, PATCH, DELETE | off |
/schemas/core\.schema | GET, POST | off |
Create¶
The schema document is of type core.schema and must contain attributes:
- target - document type to target
- content - JSON schema
Example API request creating core.schema document:
curl -H 'Content-Type: application/vnd.api+json' \
-H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-d '
{
"data": {
"type": "core.schema",
"attributes": {
"target": "car",
"content": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 3
},
"mileage": {
"type": "integer",
"minimum": 0
}
},
"required": [ "name" ],
"additionalProperties": true
}
},
"required": [ "attributes" ]
}
}
}
}' \
-X POST https://api.jazer.io/schemas/core.schema
Schema targets document type car with required attribute name of type string with a minimum length of 3 characters and optional attribute mileage of type integer with a minimum value of 0. Additional attributes are allowed.
Note
It is not advisable to edit in the dashboard schemas created via an API. Since API provides freedom to structure JSON schema as one likes, the editor might not understand used structure. However, when the structure is the same as editors it is safe to edit schema using the dashboard.
Read¶
Fetching a schema by ID can be achieved using following API request:
curl -H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-X GET https://api.jazer.io/schemas/core.schema/<SCHEMA_ID>
Update¶
Example API request to update existing schema content:
curl -H 'Content-Type: application/vnd.api+json' \
-H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-d '
{
"data": {
"type": "core.schema",
"id": "<SCHEMA_ID>",
"attributes": {
"content": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 5
},
"mileage": {
"type": "integer",
"minimum": 0
}
},
"required": [ "name" ],
"additionalProperties": true
}
},
"required": [ "attributes" ]
}
}
}
}' \
-X PATCH https://api.jazer.io/schemas/core.schema/<SCHEMA_ID>
Delete¶
To remove schema use following API request:
curl -H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-X DELETE https://api.jazer.io/schemas/core.schema/<SCHEMA_ID>
Search¶
Search is enabled on schemas endpoint. API request to fetch schemas is:
curl -H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-X GET https://api.jazer.io/schemas/core.schema
Search features like filtering, sorting, pagination etc. are available as is Search in resources module.
Example¶
Following API request creates a schema for product document. The document requires attributes: name, quantity, price, available and can have attribute tags. Each attribute has some restrictions based on a business logic. Relationship category is required with type product-category and relationship similar is optional with type product.
curl -H 'Content-Type: application/vnd.api+json' \
-H 'Accept: application/vnd.api+json' \
-H 'api-key: <YOUR_API_KEY_SECURE_ID>' \
-H 'application-id: <YOUR_APPLICATION_ID>' \
-d '
{
"data": {
"type": "core.schema",
"attributes": {
"target": "product",
"content": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 3
},
"quantity": {
"type": "integer",
"minimum": 0,
"maximum": 1000
},
"price": {
"type": "number",
"minimum": 1,
"maximum": 10000,
"multipleOf": 0.01
},
"available": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 0,
"maxItems": 5,
"uniqueItems": true
}
},
"required": [ "name", "quantity", "price", "available" ],
"additionalProperties": false
},
"relationships": {
"type": "object",
"properties": {
"category": {
"oneOf": [
{
"type": "null"
},
{
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"type": {
"type": "string",
"pattern": "^product-category$"
}
}
}
}
}
]
},
"similar": {
"oneOf": [
{
"type": "null"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"pattern": "^product$"
}
}
}
}
}
}
]
}
},
"required": [ "category" ],
"additionalProperties": false
}
},
"required": [ "attributes", "relationships" ]
}
}
}
}' \
-X POST https://api.jazer.io/schemas/core.schema