Skip to content

Commit

Permalink
Fixes from testing various schemas:
Browse files Browse the repository at this point in the history
- Allow not requesting dates and instead filling with a static value (openai does not support date format in json schema)
- Add support for converting children lists to FieldValue::Objects types
  • Loading branch information
jesseditson committed Feb 20, 2025
1 parent 1dcc519 commit 7da3895
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ thiserror = "1.0.56"
zip = { version = "0.6.6", default-features = false, features = ["deflate"] }
toml_datetime = "0.6.5"
tracing = "0.1.37"
time = { version = "0.3.36", features = ["local-offset"] }
time = { version = "0.3.36", features = ["local-offset", "formatting"] }
semver = "1.0.22"
once_cell = "1.19.0"
data-encoding = "2.5.0"
Expand Down
30 changes: 27 additions & 3 deletions src/fields/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,48 @@ impl FieldType {
let mut schema = serde_json::Map::new();
schema.insert("description".into(), description.into());
schema.insert("type".into(), "string".into());
schema.insert("format".into(), "date".into());
if let Some(date) = options.set_dates_to {
schema.insert(
"const".into(),
date.format(
&time::format_description::parse("YYYY-MM-DD HH:MM:SS").unwrap(),
)
.unwrap()
.into(),
);
} else {
schema.insert("format".into(), "date".into());
}
schema
} else {
let mut schema = serde_json::Map::new();
schema.insert("description".into(), description.into());
// Simple types
let mut is_object = false;
schema.insert(
"type".into(),
match self {
Self::String => "string".into(),
Self::Number => "number".into(),
Self::Markdown => "string".into(),
Self::Boolean => "boolean".into(),
// At some point, we should support providing a schema for meta types
Self::Meta => "object".into(),
// At some point, we should support providing a
// schema for meta types, which would require
// either inferring types based on validation or
// allowing the template to directly provide a
// schema for a given type.
Self::Meta => {
is_object = true;
"object".into()
}
_ => panic!("don't know how to parse a schema from {:?}", self),
},
);
if is_object {
schema.insert("additionalProperties".into(), false.into());
schema.insert("properties".into(), serde_json::json!({}));
schema.insert("required".into(), serde_json::json!([]));
}
schema
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/fields/field_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,21 @@ impl From<&serde_json::Value> for FieldValue {
Err(_) => FieldValue::Meta(Meta::from(o)),
}
}
serde_json::Value::Array(_) => panic!("cannot convert from json array to FieldValue"),
serde_json::Value::Array(v) => FieldValue::Objects(
v.iter()
.map(|val| {
let mut map = BTreeMap::new();
if let Some(obj) = val.as_object() {
for (k, v) in obj.iter() {
map.insert(k.to_string(), FieldValue::from(v));
}
} else {
panic!("Invalid value {} for child", val);
}
map
})
.collect(),
),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/json_schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{BTreeMap, HashMap, HashSet};

use serde_json::json;
use time::Date;

use crate::{fields::FieldType, ObjectDefinition};

Expand All @@ -10,6 +11,9 @@ pub type ObjectSchema = serde_json::Map<String, serde_json::Value>;
pub struct ObjectSchemaOptions {
pub omit_file_types: bool,
pub all_fields_required: bool,
// If a "date" format isn't supported, this option allows setting them to a
// static value.
pub set_dates_to: Option<Date>,
pub name: Option<String>,
pub property_overrides: HashMap<FieldType, serde_json::Map<String, serde_json::Value>>,
}
Expand Down Expand Up @@ -38,6 +42,10 @@ impl ObjectSchemaOptions {
self.name = Some(name);
self
}
pub fn with_date(mut self, date: Date) -> Self {
self.set_dates_to = Some(date);
self
}
}

pub fn generate_root_json_schema(
Expand Down

0 comments on commit 7da3895

Please sign in to comment.