[−][src]Macro hdk::entry
The entry
macro is a helper for creating ValidatingEntryType
definitions
for use within the define_zome macro.
It has 6 component parts:
- name:
name
is simply the descriptive name of the entry type, such as "post", or "user". It is what must be given as theentry_type_name
argument when calling commit_entry and the other data read/write functions. - description:
description
is something that is primarily for human readers of your code, just describe this entry type - sharing:
sharing
defines what distribution over the DHT, or not, occurs with entries of this type, possible values are defined in the Sharing enum - native_type:
native_type
references a given Rust struct, which provides a clear schema for entries of this type. - validation_package:
validation_package
is a special identifier, which declares which data is required from peers when attempting to validate entries of this type. Possible values are found within ValidationPackageDefinition - validation:
validation
is a callback function which will be called any time that a source chain action is taken relating to this entry type, such as commit_entry, update_entry, remove_entry. It always expects two arguments, the first of which is the entry attempting to be validated, the second is the validationcontext
, which offers a variety of metadata useful for validation.
Examples
The following is a standalone Rust file that exports a function which can be called
to get a ValidatingEntryType
of a "post".
use boolinator::*; use hdk::{ self, entry_definition::ValidatingEntryType, holochain_dna::zome::entry_types::Sharing }; use serde_json; #[derive(Serialize, Deserialize)] pub struct Post { content: String, date_created: String, } pub fn definition() -> ValidatingEntryType { entry!( name: "post", description: "a short social media style sharing of content", sharing: Sharing::Public, native_type: Post, validation_package: || { hdk::ValidationPackageDefinition::ChainFull }, validation: |post: Post, _ctx: hdk::ValidationData| { (post.content.len() < 280) .ok_or_else(|| String::from("Content too long")) } ) }