With Rivet composability, you are able to reuse fundamental pieces of your contracts, such as data types, to make them much more maintainable. We do this by leveraging CommonJS modules and compiling your contracts to JSON.
Composing with shared types
JSON Schema has a limited number of data types that it supports. For example, string, number, boolean. However, you can build custom data type validation using the pattern
key in JSON schema.
Custom Data Types
You can create custom data types to share between your contracts. If you need them on multiple applications, consider using an npm module.
Custom animal
data type:
Copy // types/animal.js
module . exports = {
id : 'types.animal' ,
type : 'string' ,
pattern : 'cat|dog'
};
Usage:
Copy // example.contract.js
const animal = require ( './types/animal' );
module . exports = {
title : 'Example' ,
type : 'object' ,
properties : {
pet : animal ,
} ,
required : [ 'pet' ]
};
Rivet default types
Rivet ships with a few pre-defined data types .
Copy const { types } = require ( 'rivet' );
module . exports = {
title : 'Example'
type: 'object' ,
properties : {
email : types .email ,
phone : types .phone ,
} ,
required : [ 'email' , 'phone' ]
};
Default Types
Below are the pre-defined types and their definitions.
email
Copy {
"id" : "types.email" ,
"type" : "string" ,
"format" : "email" ,
"pattern" : "^(([^<>()\\\[\\]\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"
}
Example Matches:
Copy bob@sho.rt
bob-villa@sho.rt
bob@example.com
bob.villa@example.com
bob+villa@example.com
phone
Copy {
"id" : "types.phone" ,
"type" : "string" ,
"pattern" : "^(?:(?:\\+?1\\s*(?:[.-]\\s*)?)?(?:\\(\\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\s*\\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\\s*(?:[.-]\\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\\s*(?:[.-]\\s*)?([0-9]{4})(?:\\s*(?:#|x\\.?|ext\\.?|extension)\\s*(\\d+))?$"
}
Example Matches:
Copy 12345678900
+12345678900
2345678900
(234) 567-8900
(555)-555-5555
555-555-5555
+1-555-532-3455
jwt
Copy {
"id" : "types.jwt" ,
"type" : "string" ,
"pattern" : "^[A-Za-z0-9-_=]+\\.[A-Za-z0-9-_=]+\\.?[A-Za-z0-9-_.+/=]*$"
}
Example Matches:
Copy ABC3EFGH.IJKLMN7PQRSTU.VWXYZ1234567890
uri
Copy {
"id" : "types.uri" ,
"type" : "string" ,
"pattern": "([A-Za-z][A-Za-z0-9+\\-.]*):(?:(//)(?:((?:[A-Za-z0-9\\-._~!$&'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&'()*+,;=:]+)\\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-Za-z0-9\\-._~!$&'()*+,;=]|%[0-9A-Fa-f]{2})*))(?::([0-9]*))?((?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)|/((?:(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)?)|((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)|)(?:\\?((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?(?:\\#((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?"
}
Example Matches:
Copy http://foo.bar
https://foo.bar
http://foo.bar/baz
https://foo.bar/baz
http://foo.bar:3000
http://foo.bar:3000/baz
git://foo.bar/baz
ssh://git@foo.bar/baz
ssh://git@foo.bar/baz.git
uuid
Copy {
"id" : "types.uuid" ,
"type" : "string" ,
"format" : "uuid" ,
"pattern" : "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
}
Example Matches:
Copy efbf1234-1242-3fd1-8d9c-94d8c3a22ec7
14f114e8-449b-4bb8-b6b6-e0fe48b650e5
39b88796-8b6b-4f76-aacd-a530235f85b8
623bf773-a86f-4371-81c6-b4133cc67c73
69dab668-bcb6-40f9-90b2-0d3181da0987
9f20d246-bc75-4a8b-b423-9a238a7ea151
756be421-8985-4c0b-adc8-3c65c5807622
ff984090-387d-4cc8-b9f9-1f4e348a9170
944595f4-8748-4ae7-bd4c-4de44e1cc2b3
ff5d5b00-dc27-4ea7-a19f-8f934cee64f8
3fab55f4-a235-479c-8d79-60eba475390f
72308901-0849-4fb3-89a7-5e332672e785
a8ab4e37-9786-46a3-955b-7c4fd5ed4f70