Data Contracts#
Overview#
Data contracts define the schema (structure) of data an application will store on Dash Platform. Contracts are described using JSON Schema which allows the platform to validate the submitted contract-related data.
The following sections provide details that developers need to construct valid contracts. All data contracts must define one or more documents that conform to the general data contract constraints.
Documents#
The documents
object defines each type of document required by the data contract. At a minimum, a document must consist of 1 or more properties. Documents may also define indices and a list of required properties. The additionalProperties
properties keyword must be included as described in the constraints section.
The following example shows a minimal documents
object defining a single document (note
) with one property (message
).
{
"note": {
"properties": {
"message": {
"type": "string",
"position": 0
}
},
"additionalProperties": false
}
}
Document Properties#
The properties
object defines each field that a document will use. Each field consists of an object that, at a minimum, must define its data type
(string
, number
, integer
, boolean
, array
, object
) and a position
.
Fields may also apply a variety of optional JSON Schema constraints related to the format, range, length, etc. of the data. A full explanation of JSON Schema capabilities is beyond the scope of this document. For more information regarding its data types and the constraints that can be applied, please refer to the JSON Schema reference documentation.
Assigning property position
#
Each property in a level must be assigned a unique position
value, with ordering starting at zero and incrementing with each property. When using nested objects, position counting resets to zero for each level. This structure supports backward compatibility in data contracts by ensuring consistent ordering for serialization and deserialization processes.
Special requirements for object
properties#
The object
type cannot be an empty object but must have one or more defined properties. For example, the body
property shown below is an object containing a single string property (objectProperty
):
const contractDocuments = {
message: {
type: "object",
properties: {
body: {
type: "object",
position: 0,
properties: {
objectProperty: {
type: "string",
"position": 0
},
},
additionalProperties: false,
},
header: {
type: "string",
"position": 1
}
},
additionalProperties: false
}
};
Property Constraints#
There are a variety of constraints currently defined for performance and security reasons.
Description |
Value |
---|---|
Minimum number of properties |
|
Maximum number of properties |
|
Minimum property name length |
1 (Note: minimum length was 3 prior to v0.23) |
Maximum property name length |
|
Property name characters |
Alphanumeric ( |
Prior to Dash Platform v0.23 there were stricter limitations on minimum property name length and the characters that could be used in property names.
Required Properties (Optional)#
Each document may have some fields that are required for the document to be valid and other optional fields. Required fields are defined via the required
array, which contains a list of the field names that must be present in the document. The required
object should only be included for documents with at least one required property.
"required": [
"<field name a>",
"<field name b>"
]
Example
The following example (excerpt from the DPNS contract’s domain
document) demonstrates a document that has 6 required fields:
"required": [
"nameHash",
"label",
"normalizedLabel",
"normalizedParentDomainName",
"preorderSalt",
"records"
],
Document Indices#
Document indices may be defined if indexing on document fields is required. The indices
object should only be included for documents with at least one index.
The indices
array consists of:
One or more objects that each contain:
A unique
name
for the indexA
properties
array composed of a<field name: sort order>
object for each document field that is part of the index (sort order:asc
only for Dash Platform v0.23)An (optional)
unique
element that determines if duplicate values are allowed for the document
🚧 Compound Indices
When defining an index with multiple properties (i.e a compound index), the order in which the properties are listed is important. Refer to the mongoDB documentation for details regarding the significance of the order as it relates to querying capabilities. Dash uses GroveDB, which works similarly but does require listing all the index’s fields in query order by statements.
"indices": [
{
"properties": [
{ "<field name a>": "<asc"|"desc>" },
{ "<field name b>": "<asc"|"desc>" }
],
"unique": true|false
},
{
"properties": [
{ "<field name c>": "<asc"|"desc>" },
],
}
]
Index Constraints#
For performance and security reasons, indices have the following constraints. These constraints are subject to change over time.
Description |
Value |
---|---|
Minimum/maximum length of index |
|
Maximum number of indices |
|
Maximum number of unique indices |
|
Maximum number of properties in a single index |
|
Maximum length of indexed string property |
|
Note: Dash Platform v0.22+. does not allow indices for arrays |
|
Note: Dash Platform v0.22+. does not allow indices for arrays |
|
Usage of |
N/A |
Example
The following example (excerpt from the DPNS contract’s preorder
document) creates an index on saltedDomainHash
that also enforces uniqueness across all documents of that type:
"indices": [
{
"properties": [
{ "saltedDomainHash": "asc" }
],
"unique": true
}
],
Full Document Syntax#
This example syntax shows the structure of a documents object that defines two documents, an index, and a required field.
{
"<document name a>": {
"type": "object",
"properties": {
"<field name b>": {
"type": "<field data type>",
"position": "<number>"
},
"<field name c>": {
"type": "<field data type>",
"position": "<number>"
},
},
"indices": [
{
"name": "<index name>",
"properties": [
{
"<field name c>": "asc"
}
],
"unique": true|false
},
],
"required": [
"<field name c>"
]
"additionalProperties": false
},
"<document name x>": {
"type": "object",
"properties": {
"<property name y>": {
"type": "<property data type>",
"position": "<number>"
},
"<property name z>": {
"type": "<property data type>",
"position": "<number>"
},
},
"additionalProperties": false
},
}
General Constraints#
There are a variety of constraints currently defined for performance and security reasons. The following constraints are applicable to all aspects of data contracts. Unless otherwise noted, these constraints are defined in the platform’s JSON Schema rules (e.g. rs-dpp data contract meta schema).
Keyword#
🚧
The
$ref
keyword has been disabled since Platform v0.22.
Keyword |
Constraint |
---|---|
|
Restricted - cannot be used (defined in DPP logic) |
|
Restricted - cannot be used (defined in DPP logic) |
|
|
|
|
|
|
|
Disabled for data contracts |
|
Disabled for data contracts |
|
Not supported. Use |
|
Not supported. Use |
|
Restricted - cannot be used for data contracts |
|
Accept only RE2 compatible regular expressions (defined in DPP logic) |
Data Size#
Note: These constraints are defined in the Dash Platform Protocol logic (not in JSON Schema).
All serialized data (including state transitions) is limited to a maximum size of 16 KB.
Additional Properties#
Although JSON Schema allows additional, undefined properties by default, they are not allowed in Dash Platform data contracts. Data contract validation will fail if they are not explicitly forbidden using the additionalProperties
keyword anywhere properties
are defined (including within document properties of type object
).
Include the following at the same level as the properties
keyword to ensure proper validation:
"additionalProperties": false