Skip to content

Commit

Permalink
fix: type names in json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
raimundo-henriques committed Sep 19, 2024
1 parent 5e72bcb commit f136716
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 19 deletions.
47 changes: 33 additions & 14 deletions schema/data-model-schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ProductFootprint_for_AnyValue",
"title": "ProductFootprint",
"description": "Data Type \"ProductFootprint\" of Tech Spec Version 2",
"type": "object",
"required": [
Expand Down Expand Up @@ -38,7 +38,7 @@
"null"
],
"items": {
"$ref": "#/definitions/DataModelExtension_for_AnyValue"
"$ref": "#/definitions/DataModelExtension"
}
},
"id": {
Expand Down Expand Up @@ -258,7 +258,6 @@
"ipccCharacterizationFactorsSources",
"pCfExcludingBiogenic",
"packagingEmissionsIncluded",
"productOrSectorSpecificRules",
"referencePeriodEnd",
"referencePeriodStart",
"unitaryProductAmount"
Expand Down Expand Up @@ -427,7 +426,14 @@
]
},
"productOrSectorSpecificRules": {
"$ref": "#/definitions/ProductOrSectorSpecificRuleSet"
"anyOf": [
{
"$ref": "#/definitions/ProductOrSectorSpecificRuleSet"
},
{
"type": "null"
}
]
},
"referencePeriodEnd": {
"type": "string",
Expand Down Expand Up @@ -478,9 +484,14 @@
"description": "Data Type \"CrossSectoralStandard\" of Spec Version 2",
"type": "string",
"enum": [
"GHG Protocol Product standard",
"ISO Standard 14067",
"ISO Standard 14044"
"GHGP Product",
"ISO14067",
"ISO14044",
"ISO14083",
"ISO14040-44",
"PEF",
"PACT Methodology 3.0",
"PAS2050"
]
},
"CrossSectoralStandardSet": {
Expand All @@ -489,23 +500,31 @@
"description": "Data Type \"CrossSectoralStandard\" of Spec Version 2",
"type": "string",
"enum": [
"GHG Protocol Product standard",
"ISO Standard 14067",
"ISO Standard 14044"
"GHGP Product",
"ISO14067",
"ISO14044",
"ISO14083",
"ISO14040-44",
"PEF",
"PACT Methodology 3.0",
"PAS2050"
]
},
"minItems": 1,
"uniqueItems": true
},
"DataModelExtension_for_AnyValue": {
"type": "object",
"DataModelExtension": {
"title": "DataModelExtension",
"required": [
"data",
"dataSchema",
"specVersion"
],
"properties": {
"data": true,
"data": {
"type": "object",
"additionalProperties": true
},
"dataSchema": {
"type": "string"
},
Expand Down Expand Up @@ -798,4 +817,4 @@
"pattern": "^\\d+\\.\\d+\\.\\d+(-\\d{8})?$"
}
}
}
}
81 changes: 77 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
//! emission data at product level.
//!
//! See https://wbcsd.github.io/data-exchange-protocol/v2 for further details.
use std::collections::BTreeMap;

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use schemars::schema::{ArrayValidation, NumberValidation, Schema, StringValidation};
use schemars::schema::{
ArrayValidation, InstanceType, Metadata, NumberValidation, ObjectValidation, Schema,
SchemaObject, SingleOrVec, StringValidation,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase")]
/// Data Type "ProductFootprint" of Tech Spec Version 2
pub struct ProductFootprint<T> {
pub struct ProductFootprint<T: JsonSchema> {
pub id: PfId,
pub spec_version: SpecVersionString,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -441,13 +446,14 @@ pub enum AssuranceBoundary {
CradleToGate,
}

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DataModelExtension<T> {
pub struct DataModelExtension<T: JsonSchema> {
pub spec_version: SpecVersionString,
pub data_schema: String, // Replace String with URL
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation: Option<String>, // Replace String with URL

pub data: T,
}

Expand Down Expand Up @@ -884,6 +890,73 @@ impl JsonSchema for PfId {
}
}

impl<T: JsonSchema> JsonSchema for DataModelExtension<T> {
fn schema_name() -> String {
"DataModelExtension".into()
}

fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
let mut properties = BTreeMap::new();
properties.insert(
"data".to_string(),
Schema::Object(SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
additional_properties: Some(Box::new(Schema::Bool(true))),
..Default::default()
})),
..Default::default()
}),
);

properties.insert(
"dataSchema".to_string(),
Schema::Object(SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}),
);
properties.insert(
"documentation".to_string(),
Schema::Object(SchemaObject {
instance_type: Some(SingleOrVec::Vec(vec![
InstanceType::String,
InstanceType::Null,
])),
..Default::default()
}),
);
properties.insert(
"specVersion".to_string(),
Schema::Object(SchemaObject {
reference: Some("#/definitions/VersionString".to_string()),
..Default::default()
}),
);

let schema_object = SchemaObject {
metadata: Some(Box::new(Metadata {
title: Some(Self::schema_name()),
..Default::default()
})),
object: Some(Box::new(ObjectValidation {
properties,
required: vec![
"data".to_string(),
"dataSchema".to_string(),
"specVersion".to_string(),
]
.into_iter()
.collect(),
..Default::default()
})),
..Default::default()
};

Schema::Object(schema_object)
}
}

fn json_set_schema<T: JsonSchema>(
gen: &mut schemars::gen::SchemaGenerator,
min_items: Option<u32>,
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use std::fs::File;
use std::io::{Error, Write};

fn main() -> Result<(), Error> {
let schema = schema_for!(ProductFootprint<Value>);
let mut schema = schema_for!(ProductFootprint<Value>);

if let Some(metadata) = schema.schema.metadata.as_mut() {
metadata.title = Some("ProductFootprint".to_string());
}
let schema_json = to_string_pretty(&schema).expect("Failed to serialize schema");

let mut file = File::create("./schema/data-model-schema.json")?;
Expand Down

0 comments on commit f136716

Please sign in to comment.