Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dpp): token meta schema #2378

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions packages/rs-dpp/schema/meta_schemas/token/v0/token-meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/dashpay/platform/blob/master/packages/rs-dpp/schema/meta_schemas/document/v0/document-meta.json",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Incorrect $id URL path

The schema's $id URL references document-meta.json instead of token-meta.json.

Apply this fix:

-  "$id": "https://github.com/dashpay/platform/blob/master/packages/rs-dpp/schema/meta_schemas/document/v0/document-meta.json",
+  "$id": "https://github.com/dashpay/platform/blob/master/packages/rs-dpp/schema/meta_schemas/token/v0/token-meta.json",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"$id": "https://github.com/dashpay/platform/blob/master/packages/rs-dpp/schema/meta_schemas/document/v0/document-meta.json",
"$id": "https://github.com/dashpay/platform/blob/master/packages/rs-dpp/schema/meta_schemas/token/v0/token-meta.json",

"type": "object",
"$defs": {
"localization": {
"type": "string",
"pattern": "^[\\p{L}\\p{N}]*$",
"minLength": 1,
"maxLength": 64,
"$comment": "Allow only alphanumeric characters"
}
},
"properties": {
"shouldCapitalize": {
"type": "boolean",
"description": "TODO"
},
"localizations": {
"type": "object",
"description": "TODO",
"additionalProperties": {
"type": "object",
"properties": {
"singular": {
"$ref": "#/$defs/localization"
},
"plural": {
"$ref": "#/$defs/localization"
}
},
"required": ["singular", "plural"],
"additionalProperties": false
},
"maxProperties": 255,
"minProperties": 1
},
"maintainer": {
"type": "array",
"contentMediaType": "application/x.dash.dpp.identifier",
"byteArray": true,
"minItems": 32,
"maxItems": 32,
"description": "TODO"
},
"initialSupply": {
"type": "integer",
"minimum": 0,
"description": "TODO"
},
"decimals": {
"type": "integer",
"minimum": 0,
"description": "TODO"
},
"maxSupply": {
"type": "integer",
"minimum": 1,
"description": "TODO"
},
"permissions": {
"type": "object",
"properties": {
"maintainer": {
"type": "object",
"properties": {
"canMint": {
"type": "boolean"
},
"canBurn": {
"type": "boolean"
}
},
"additionalProperties": false,
"required": ["canBurn", "canMint"]
}
},
"additionalProperties": false,
"minProperties": 1,
"description": "TODO"
},
"description": {
"type": "string",
"maxLength": 1024,
"description": "Token description"
},
"metadata": {
"type": "object",
"propertyNames": {
"type": "string",
"maxLength": 255,
},
"additionalProperties": {
"type": "string",
"maxLength": 1024,
},
"minProperties": 1,
"maxProperties": 255,
"description": "Token arbitrary metadata",
}
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
},
"required": [
"initialSupply",
"decimals"
]
}
65 changes: 61 additions & 4 deletions packages/rs-dpp/src/validation/meta_validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ lazy_static! {
"../../../schema/meta_schemas/draft2020-12/meta/content.json"
))
.expect("Valid schema!");
static ref DATA_CONTRACT_V0: Value = serde_json::from_str::<Value>(include_str!(
static ref DOCUMENT_META_JSON_V0: Value = serde_json::from_str::<Value>(include_str!(
"../../../schema/meta_schemas/document/v0/document-meta.json"
))
.unwrap();
static ref TOKEN_META_JSON_V0: Value = serde_json::from_str::<Value>(include_str!(
"../../../schema/meta_schemas/token/v0/token-meta.json"
))
.unwrap();

pub static ref DRAFT_202012_META_SCHEMA: JSONSchema = JSONSchema::options()
.with_draft(Draft::Draft202012)
Expand Down Expand Up @@ -85,8 +89,7 @@ lazy_static! {
.compile(&DRAFT202012)
.expect("Invalid data contract schema");


// Compiled version of data contract meta schema
// Compiled version of document meta schema
pub static ref DOCUMENT_META_SCHEMA_V0: JSONSchema = JSONSchema::options()
.with_keyword(
"byteArray",
Expand Down Expand Up @@ -137,6 +140,60 @@ lazy_static! {
DRAFT202012.clone(),
)
.to_owned()
.compile(&DATA_CONTRACT_V0)
.compile(&DOCUMENT_META_JSON_V0)
.expect("Invalid data contract schema");

// Compiled version of token meta schema
pub static ref TOKEN_META_SCHEMA_V0: JSONSchema = JSONSchema::options()
.with_keyword(
"byteArray",
|_, _, _| Ok(Box::new(ByteArrayKeyword)),
)
.with_patterns_regex_engine(RegexEngine::Regex(RegexOptions {
size_limit: Some(5 * (1 << 20)),
..Default::default()
}))
.should_ignore_unknown_formats(false)
.should_validate_formats(true)
.with_patterns_regex_engine(RegexEngine::Regex(Default::default()))
.with_draft(Draft::Draft202012)
.with_document(
"https://json-schema.org/draft/2020-12/meta/applicator".to_string(),
DRAFT202012_APPLICATOR.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/core".to_string(),
DRAFT202012_CORE.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/applicator".to_string(),
DRAFT202012_APPLICATOR.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/unevaluated".to_string(),
DRAFT202012_UNEVALUATED.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/validation".to_string(),
DRAFT202012_VALIDATION.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/meta-data".to_string(),
DRAFT202012_META_DATA.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/format-annotation".to_string(),
DRAFT202012_FORMAT_ANNOTATION.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/meta/content".to_string(),
DRAFT202012_CONTENT.clone(),
)
.with_document(
"https://json-schema.org/draft/2020-12/schema".to_string(),
DRAFT202012.clone(),
)
.to_owned()
.compile(&TOKEN_META_JSON_V0)
Comment on lines +146 to +197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider reducing code duplication in schema configurations

The configuration for TOKEN_META_SCHEMA_V0 is identical to DOCUMENT_META_SCHEMA_V0. This duplication increases maintenance overhead.

Consider extracting the common configuration into a helper function:

fn create_base_schema_options() -> jsonschema::CompilationOptions {
    JSONSchema::options()
        .with_keyword(
            "byteArray",
            |_, _, _| Ok(Box::new(ByteArrayKeyword)),
        )
        .with_patterns_regex_engine(RegexEngine::Regex(RegexOptions {
            size_limit: Some(5 * (1 << 20)),
            ..Default::default()
        }))
        .should_ignore_unknown_formats(false)
        .should_validate_formats(true)
        .with_patterns_regex_engine(RegexEngine::Regex(Default::default()))
        .with_draft(Draft::Draft202012)
        // Add all the .with_document calls here
        .to_owned()
}

// Usage:
pub static ref TOKEN_META_SCHEMA_V0: JSONSchema = create_base_schema_options()
    .compile(&TOKEN_META_JSON_V0)
    .expect("Invalid token meta schema");

.expect("Invalid data contract schema");
}