Skip to content

Commit b817faf

Browse files
fix: type names in json schema
1 parent 5e72bcb commit b817faf

File tree

3 files changed

+99
-18
lines changed

3 files changed

+99
-18
lines changed

schema/data-model-schema.json

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"title": "ProductFootprint_for_AnyValue",
3+
"title": "ProductFootprint",
44
"description": "Data Type \"ProductFootprint\" of Tech Spec Version 2",
55
"type": "object",
66
"required": [
@@ -38,7 +38,7 @@
3838
"null"
3939
],
4040
"items": {
41-
"$ref": "#/definitions/DataModelExtension_for_AnyValue"
41+
"$ref": "#/definitions/DataModelExtension"
4242
}
4343
},
4444
"id": {
@@ -258,7 +258,6 @@
258258
"ipccCharacterizationFactorsSources",
259259
"pCfExcludingBiogenic",
260260
"packagingEmissionsIncluded",
261-
"productOrSectorSpecificRules",
262261
"referencePeriodEnd",
263262
"referencePeriodStart",
264263
"unitaryProductAmount"
@@ -427,7 +426,14 @@
427426
]
428427
},
429428
"productOrSectorSpecificRules": {
430-
"$ref": "#/definitions/ProductOrSectorSpecificRuleSet"
429+
"anyOf": [
430+
{
431+
"$ref": "#/definitions/ProductOrSectorSpecificRuleSet"
432+
},
433+
{
434+
"type": "null"
435+
}
436+
]
431437
},
432438
"referencePeriodEnd": {
433439
"type": "string",
@@ -478,9 +484,14 @@
478484
"description": "Data Type \"CrossSectoralStandard\" of Spec Version 2",
479485
"type": "string",
480486
"enum": [
481-
"GHG Protocol Product standard",
482-
"ISO Standard 14067",
483-
"ISO Standard 14044"
487+
"GHGP Product",
488+
"ISO14067",
489+
"ISO14044",
490+
"ISO14083",
491+
"ISO14040-44",
492+
"PEF",
493+
"PACT Methodology 3.0",
494+
"PAS2050"
484495
]
485496
},
486497
"CrossSectoralStandardSet": {
@@ -489,16 +500,21 @@
489500
"description": "Data Type \"CrossSectoralStandard\" of Spec Version 2",
490501
"type": "string",
491502
"enum": [
492-
"GHG Protocol Product standard",
493-
"ISO Standard 14067",
494-
"ISO Standard 14044"
503+
"GHGP Product",
504+
"ISO14067",
505+
"ISO14044",
506+
"ISO14083",
507+
"ISO14040-44",
508+
"PEF",
509+
"PACT Methodology 3.0",
510+
"PAS2050"
495511
]
496512
},
497513
"minItems": 1,
498514
"uniqueItems": true
499515
},
500-
"DataModelExtension_for_AnyValue": {
501-
"type": "object",
516+
"DataModelExtension": {
517+
"title": "DataModelExtension",
502518
"required": [
503519
"data",
504520
"dataSchema",
@@ -798,4 +814,4 @@
798814
"pattern": "^\\d+\\.\\d+\\.\\d+(-\\d{8})?$"
799815
}
800816
}
801-
}
817+
}

src/lib.rs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
//! emission data at product level.
1010
//!
1111
//! See https://wbcsd.github.io/data-exchange-protocol/v2 for further details.
12+
use std::collections::BTreeMap;
13+
1214
use chrono::{DateTime, Utc};
1315
use rust_decimal::Decimal;
14-
use schemars::schema::{ArrayValidation, NumberValidation, Schema, StringValidation};
16+
use schemars::schema::{
17+
ArrayValidation, InstanceType, Metadata, NumberValidation, ObjectValidation, Schema,
18+
SchemaObject, SingleOrVec, StringValidation,
19+
};
1520
use schemars::JsonSchema;
1621
use serde::{Deserialize, Serialize};
1722
use uuid::Uuid;
1823

1924
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
2025
#[serde(rename_all = "camelCase")]
2126
/// Data Type "ProductFootprint" of Tech Spec Version 2
22-
pub struct ProductFootprint<T> {
27+
pub struct ProductFootprint<T: JsonSchema> {
2328
pub id: PfId,
2429
pub spec_version: SpecVersionString,
2530
#[serde(skip_serializing_if = "Option::is_none")]
@@ -441,13 +446,14 @@ pub enum AssuranceBoundary {
441446
CradleToGate,
442447
}
443448

444-
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
449+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
445450
#[serde(rename_all = "camelCase")]
446-
pub struct DataModelExtension<T> {
451+
pub struct DataModelExtension<T: JsonSchema> {
447452
pub spec_version: SpecVersionString,
448453
pub data_schema: String, // Replace String with URL
449454
#[serde(skip_serializing_if = "Option::is_none")]
450455
pub documentation: Option<String>, // Replace String with URL
456+
451457
pub data: T,
452458
}
453459

@@ -884,6 +890,62 @@ impl JsonSchema for PfId {
884890
}
885891
}
886892

893+
impl<T: JsonSchema> JsonSchema for DataModelExtension<T> {
894+
fn schema_name() -> String {
895+
"DataModelExtension".into()
896+
}
897+
898+
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
899+
let mut properties = BTreeMap::new();
900+
properties.insert("data".to_string(), T::json_schema(gen));
901+
properties.insert(
902+
"dataSchema".to_string(),
903+
Schema::Object(SchemaObject {
904+
instance_type: Some(InstanceType::String.into()),
905+
..Default::default()
906+
}),
907+
);
908+
properties.insert(
909+
"documentation".to_string(),
910+
Schema::Object(SchemaObject {
911+
instance_type: Some(SingleOrVec::Vec(vec![
912+
InstanceType::String,
913+
InstanceType::Null,
914+
])),
915+
..Default::default()
916+
}),
917+
);
918+
properties.insert(
919+
"specVersion".to_string(),
920+
Schema::Object(SchemaObject {
921+
reference: Some("#/definitions/VersionString".to_string()),
922+
..Default::default()
923+
}),
924+
);
925+
926+
let schema_object = SchemaObject {
927+
metadata: Some(Box::new(Metadata {
928+
title: Some(Self::schema_name()),
929+
..Default::default()
930+
})),
931+
object: Some(Box::new(ObjectValidation {
932+
properties,
933+
required: vec![
934+
"data".to_string(),
935+
"dataSchema".to_string(),
936+
"specVersion".to_string(),
937+
]
938+
.into_iter()
939+
.collect(),
940+
..Default::default()
941+
})),
942+
..Default::default()
943+
};
944+
945+
Schema::Object(schema_object)
946+
}
947+
}
948+
887949
fn json_set_schema<T: JsonSchema>(
888950
gen: &mut schemars::gen::SchemaGenerator,
889951
min_items: Option<u32>,

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use std::fs::File;
55
use std::io::{Error, Write};
66

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

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

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

0 commit comments

Comments
 (0)