use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use tilo_sort::CompactSort as _;
use serde_json::{Map, Value, json};
use tilo_actions::{ActionParam, ParamType};
mod table;
pub use table::{Body, Bounds, Content, Field, Tagging, TypeSchema, Variant, build};
pub trait JsonSchema {
const OPTIONAL: bool = false;
fn json_schema() -> Value;
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
let _ = types;
crate::FieldType::Any
}
}
macro_rules! primitive {
($($ty:ty => $schema:expr, $field:ident),* $(,)?) => {
$(impl JsonSchema for $ty {
fn json_schema() -> Value {
$schema
}
fn field_type(_: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::$field
}
})*
};
}
primitive! {
bool => json!({"type": "boolean"}), Bool,
String => json!({"type": "string"}), Text,
str => json!({"type": "string"}), Text,
char => json!({"type": "string", "minLength": 1, "maxLength": 1}), Text,
f32 => json!({"type": "number"}), Float,
f64 => json!({"type": "number"}), Float,
i8 => json!({"type": "integer", "minimum": i8::MIN, "maximum": i8::MAX}), Int,
i16 => json!({"type": "integer", "minimum": i16::MIN, "maximum": i16::MAX}), Int,
i32 => json!({"type": "integer", "minimum": i32::MIN, "maximum": i32::MAX}), Int,
i64 => json!({"type": "integer"}), Int,
isize => json!({"type": "integer"}), Int,
u8 => json!({"type": "integer", "minimum": 0, "maximum": u8::MAX}), Int,
u16 => json!({"type": "integer", "minimum": 0, "maximum": u16::MAX}), Int,
u32 => json!({"type": "integer", "minimum": 0, "maximum": u32::MAX}), Int,
u64 => json!({"type": "integer", "minimum": 0}), Int,
usize => json!({"type": "integer", "minimum": 0}), Int,
serde_json::Value => json!({}), Any,
crate::Id => json!({"type": "string"}), Text,
crate::Counter => json!({"type": "integer"}), Int,
uuid::Uuid => json!({"type": "string", "format": "uuid"}), Text,
}
impl JsonSchema for tilo_action_policy::MemberId {
fn json_schema() -> Value {
let text = || json!({"type": "string"});
let kind = |name: &str| {
json!({
"type": "object",
"properties": { name: text() },
"required": [name],
"additionalProperties": false,
})
};
let agent = json!({
"type": "object",
"properties": {
"agent": {
"type": "object",
"properties": {"id": text(), "person": text()},
"required": ["id", "person"],
"additionalProperties": false,
},
},
"required": ["agent"],
"additionalProperties": false,
});
json!({"oneOf": [kind("person"), agent, kind("bot")]})
}
}
impl JsonSchema for tilo_actions::Actor {
fn json_schema() -> Value {
let kind = |name: &str, value: Value| {
json!({
"type": "object",
"properties": { name: value },
"required": [name],
"additionalProperties": false,
})
};
json!({"oneOf": [
kind("member", <tilo_action_policy::MemberId as JsonSchema>::json_schema()),
kind("automation", json!({"type": "string"})),
kind("app", json!({"type": "string"})),
kind("visitor", json!({"type": "string"})),
]})
}
}
impl JsonSchema for tilo_actions::ActionContext {
fn json_schema() -> Value {
json!({"type": "object"})
}
}
impl JsonSchema for tilo_actions::RecipientKind {
fn json_schema() -> Value {
json!({
"type": "string",
"enum": ["new-agent", "agent", "person", "conversation", "feedback"],
})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::surface::enum_type(
types,
"tilo_actions::RecipientKind",
"RecipientKind",
"Who a message sent from a window goes to.",
&[
("new-agent", "New agent", &[]),
("agent", "Agent", &[]),
("person", "Person", &[]),
("conversation", "Conversation", &[]),
("feedback", "Feedback", &[]),
],
)
}
}
impl JsonSchema for tilo_actions::Recipient {
fn json_schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"properties": {
"kind": <tilo_actions::RecipientKind as JsonSchema>::json_schema(),
"id": {"type": "string"},
"name": {"type": "string"},
"detail": {"type": ["string", "null"]},
},
"required": ["kind", "name"],
})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::surface::resource_of(
types,
"tilo_actions::Recipient",
"Recipient",
"One recipient of a message sent from a window.",
&[
(
"kind",
<tilo_actions::RecipientKind as JsonSchema>::field_type,
),
("id", |_| crate::FieldType::Text),
("name", |_| crate::FieldType::Text),
("detail", |_| {
crate::FieldType::Option(Box::new(crate::FieldType::Text))
}),
],
)
}
}
impl JsonSchema for tilo_actions::remote::ItemRef {
fn json_schema() -> Value {
let mut schema = serde_json::Map::new();
schema.insert("type".to_owned(), json!("string"));
schema.insert(
tilo_actions::params::RESOURCE_KIND_ANNOTATION.to_owned(),
json!(tilo_actions::remote::VAULT_ITEM_KIND),
);
Value::Object(schema)
}
fn field_type(_: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Resource(tilo_actions::remote::VAULT_ITEM_KIND.to_owned())
}
}
impl<T: JsonSchema> JsonSchema for Option<T> {
const OPTIONAL: bool = true;
fn json_schema() -> Value {
nullable(T::json_schema())
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Option(Box::new(T::field_type(types)))
}
}
impl<T: JsonSchema + ?Sized> JsonSchema for Box<T> {
const OPTIONAL: bool = T::OPTIONAL;
fn json_schema() -> Value {
T::json_schema()
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
T::field_type(types)
}
}
impl<T: JsonSchema + ?Sized> JsonSchema for std::sync::Arc<T> {
const OPTIONAL: bool = T::OPTIONAL;
fn json_schema() -> Value {
T::json_schema()
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
T::field_type(types)
}
}
impl<T: JsonSchema + ?Sized> JsonSchema for &T {
const OPTIONAL: bool = T::OPTIONAL;
fn json_schema() -> Value {
T::json_schema()
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
T::field_type(types)
}
}
impl<T: JsonSchema> JsonSchema for [T] {
fn json_schema() -> Value {
array(T::json_schema())
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::List(Box::new(T::field_type(types)))
}
}
impl<T: JsonSchema> JsonSchema for Vec<T> {
fn json_schema() -> Value {
array(T::json_schema())
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::List(Box::new(T::field_type(types)))
}
}
impl<T: JsonSchema, const N: usize> JsonSchema for [T; N] {
fn json_schema() -> Value {
let mut schema = array(T::json_schema());
if let Some(object) = schema.as_object_mut() {
object.insert("minItems".into(), N.into());
object.insert("maxItems".into(), N.into());
}
schema
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::List(Box::new(T::field_type(types)))
}
}
macro_rules! tuples {
($($len:literal => ($($name:ident),+)),* $(,)?) => {
$(impl<$($name: JsonSchema),+> JsonSchema for ($($name,)+) {
fn json_schema() -> Value {
tuple($len)
}
})*
};
}
tuples! {
1 => (A),
2 => (A, B),
3 => (A, B, C),
4 => (A, B, C, D),
}
impl<T: JsonSchema, S> JsonSchema for HashSet<T, S> {
fn json_schema() -> Value {
array(T::json_schema())
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Set(Box::new(T::field_type(types)))
}
}
impl<T: JsonSchema> JsonSchema for BTreeSet<T> {
fn json_schema() -> Value {
array(T::json_schema())
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Set(Box::new(T::field_type(types)))
}
}
impl<K, V: JsonSchema, S> JsonSchema for HashMap<K, V, S> {
fn json_schema() -> Value {
json!({"type": "object"})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Map(Box::new(V::field_type(types)))
}
}
impl<K, V: JsonSchema> JsonSchema for BTreeMap<K, V> {
fn json_schema() -> Value {
json!({"type": "object"})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Map(Box::new(V::field_type(types)))
}
}
impl<V: JsonSchema> JsonSchema for serde_json::Map<String, V> {
fn json_schema() -> Value {
json!({"type": "object"})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Map(Box::new(V::field_type(types)))
}
}
pub fn array(items: Value) -> Value {
let mut schema = Map::new();
schema.insert("type".into(), "array".into());
schema.insert("items".into(), items);
Value::Object(schema)
}
pub fn tuple(len: usize) -> Value {
json!({"type": "array", "minItems": len, "maxItems": len})
}
pub fn nullable(schema: Value) -> Value {
let Value::Object(mut object) = schema else {
return schema;
};
if object.is_empty() {
return Value::Object(object);
}
let simple = !object.contains_key("oneOf")
&& !object.contains_key("const")
&& !object.contains_key("enum");
match object.get_mut("type") {
Some(Value::String(name)) if simple => {
if name != "null" {
let name = std::mem::take(name);
object.insert("type".into(), json!([name, "null"]));
}
Value::Object(object)
}
Some(Value::Array(types)) if simple => {
if !types.iter().any(|name| name == "null") {
types.push("null".into());
}
Value::Object(object)
}
_ => {
let description = object.remove("description");
let mut wrapped = Map::new();
wrapped.insert(
"oneOf".into(),
json!([{"type": "null"}, Value::Object(object)]),
);
if let Some(description) = description {
wrapped.insert("description".into(), description);
}
Value::Object(wrapped)
}
}
}
pub fn range(schema: Value, minimum: Option<Value>, maximum: Option<Value>) -> Value {
let Value::Object(mut object) = schema else {
return schema;
};
if let Some(Value::Array(alternatives)) = object.get_mut("oneOf") {
for alternative in alternatives.iter_mut() {
if alternative.get("type") != Some(&json!("null")) {
*alternative = range(alternative.take(), minimum.clone(), maximum.clone());
}
}
return Value::Object(object);
}
let named = match object.get("type") {
Some(Value::String(name)) => Some(name.as_str()),
Some(Value::Array(names)) => names
.iter()
.filter_map(Value::as_str)
.find(|name| *name != "null"),
_ => None,
};
let keys = match named {
Some("string") => ("minLength", "maxLength"),
Some("array") => ("minItems", "maxItems"),
_ => ("minimum", "maximum"),
};
for (key, bound) in [(keys.0, minimum), (keys.1, maximum)] {
if let Some(bound) = bound {
object.insert(key.into(), bound);
}
}
Value::Object(object)
}
pub fn choices(schema: Value, choices: &[&str]) -> Value {
let Value::Object(mut object) = schema else {
return schema;
};
if let Some(Value::Array(alternatives)) = object.get_mut("oneOf") {
for alternative in alternatives.iter_mut() {
if alternative.get("type") != Some(&json!("null")) {
*alternative = self::choices(alternative.take(), choices);
}
}
return Value::Object(object);
}
let mut values: Vec<Value> = choices.iter().map(|choice| json!(choice)).collect();
if matches!(object.get("type"), Some(Value::Array(names)) if names.iter().any(|name| name == "null"))
{
values.push(Value::Null);
}
object.insert("enum".into(), Value::Array(values));
Value::Object(object)
}
pub fn with_default(schema: Value, default: Option<Value>) -> Value {
match (schema, default) {
(Value::Object(mut object), Some(default)) => {
object.insert("default".into(), default);
Value::Object(object)
}
(schema, _) => schema,
}
}
pub fn describe(schema: Value, description: &str) -> Value {
if description.is_empty() {
return schema;
}
match schema {
Value::Object(mut object) => {
object.insert("description".into(), description.into());
Value::Object(object)
}
other => other,
}
}
pub fn object(properties: Vec<(&str, Value, bool)>) -> Value {
let required: Vec<Value> = properties
.iter()
.filter(|(_, _, required)| *required)
.map(|(name, _, _)| Value::from(*name))
.collect();
let properties: Map<String, Value> = properties
.into_iter()
.map(|(name, schema, _)| (name.to_owned(), schema))
.collect();
let mut schema = Map::new();
schema.insert("type".into(), "object".into());
if !required.is_empty() {
schema.insert("required".into(), Value::Array(required));
}
schema.insert("properties".into(), Value::Object(properties));
schema.insert("additionalProperties".into(), false.into());
Value::Object(schema)
}
pub fn one_of(alternatives: Vec<Value>) -> Value {
let mut schema = Map::new();
schema.insert("oneOf".into(), Value::Array(alternatives));
Value::Object(schema)
}
pub fn constant(value: &str) -> Value {
json!({"type": "string", "const": value})
}
pub fn string_enum(values: &[&str]) -> Value {
json!({"type": "string", "enum": values})
}
pub fn external(name: &str, content: Value) -> Value {
object(vec![(name, content, true)])
}
pub fn adjacent(tag: &str, content_key: &str, name: &str, content: Option<Value>) -> Value {
let mut properties = vec![(tag, constant(name), true)];
if let Some(content) = content {
properties.push((content_key, content, true));
}
object(properties)
}
pub fn internal(tag: &str, name: &str, content: Value) -> Value {
let Value::Object(mut schema) = content else {
return object(vec![(tag, constant(name), true)]);
};
if let Some(Value::Object(properties)) = schema.get_mut("properties") {
properties.insert(tag.to_owned(), constant(name));
} else {
schema.insert("type".into(), "object".into());
schema.insert("properties".into(), json!({ tag: constant(name) }));
}
match schema.get_mut("required") {
Some(Value::Array(required)) => required.insert(0, tag.into()),
_ => {
schema.insert("required".into(), json!([tag]));
}
}
Value::Object(schema)
}
pub fn undescribed(schema: &Value) -> Value {
match schema {
Value::Object(object) => Value::Object(
object
.iter()
.filter(|(key, _)| key.as_str() != "description")
.map(|(key, value)| {
let value = if key == "properties" {
match value {
Value::Object(properties) => Value::Object(
properties
.iter()
.map(|(name, child)| (name.clone(), undescribed(child)))
.collect(),
),
other => other.clone(),
}
} else {
undescribed(value)
};
(key.clone(), value)
})
.collect(),
),
Value::Array(values) => Value::Array(values.iter().map(undescribed).collect()),
other => other.clone(),
}
}
pub(crate) fn param_schema(param: &ActionParam) -> Value {
let base = match param.ty {
ParamType::Text | ParamType::Secret | ParamType::Resource => json!({"type": "string"}),
ParamType::DateTime => json!({"type": "string", "format": "date-time"}),
ParamType::Integer => json!({"type": "integer"}),
ParamType::Number => json!({"type": "number"}),
ParamType::Boolean => json!({"type": "boolean"}),
ParamType::Array => json!({"type": "array"}),
ParamType::Object => json!({}),
};
let mut schema = match (param.schema.as_deref(), base) {
(Some(Value::Object(declared)), Value::Object(base)) => {
let mut merged = declared.clone();
for (key, value) in base {
merged.entry(key).or_insert(value);
}
merged
}
(_, Value::Object(base)) => base,
(_, _) => Map::new(),
};
if !param.choices.is_empty() && !schema.contains_key("enum") {
let choices = param
.choices
.iter()
.map(|choice| match param.ty {
ParamType::Text | ParamType::Secret | ParamType::Resource | ParamType::DateTime => {
Value::String(choice.to_string())
}
_ => serde_json::from_str(choice).unwrap_or_else(|_| choice.to_string().into()),
})
.collect();
schema.insert("enum".into(), Value::Array(choices));
}
let schema = describe(Value::Object(schema), ¶m.description);
if param.nullable || !param.required {
nullable(schema)
} else {
schema
}
}
pub fn args_schema(params: &[ActionParam]) -> Value {
object(
params
.iter()
.map(|param| (param.name.as_ref(), param_schema(param), param.required))
.collect(),
)
}
pub(crate) fn fold_declared(schema: &Value, declared: &BTreeMap<&str, Value>) -> Value {
match schema {
Value::Object(object) => {
let mut out = Map::new();
for (key, value) in object {
let value = match (key.as_str(), value) {
("oneOf", Value::Array(alternatives)) => {
Value::Array(fold_alternatives(alternatives, declared))
}
_ => fold_declared(value, declared),
};
out.insert(key.clone(), value);
}
Value::Object(out)
}
Value::Array(values) => Value::Array(
values
.iter()
.map(|value| fold_declared(value, declared))
.collect(),
),
other => other.clone(),
}
}
fn fold_alternatives(alternatives: &[Value], declared: &BTreeMap<&str, Value>) -> Vec<Value> {
let (mut internal, mut adjacent, mut kept) = (Vec::new(), Vec::new(), Vec::new());
for alternative in alternatives {
match restated(alternative, declared) {
Some((id, false)) => internal.push(id),
Some((id, true)) => adjacent.push(id),
None => kept.push(fold_declared(alternative, declared)),
}
}
let names = |ids: &[&str]| json!({"type": "string", "enum": ids});
if !internal.is_empty() {
kept.push(json!({
"type": "object",
"required": ["action"],
"properties": {"action": names(&internal)},
"description": "A named action: `action` is its id, the other fields its parameters",
}));
}
if !adjacent.is_empty() {
kept.push(json!({
"type": "object",
"required": ["action", "args"],
"properties": {"action": names(&adjacent), "args": {"type": "object"}},
"additionalProperties": false,
"description": "A named action: `action` is its id, `args` its parameters",
}));
}
kept
}
fn restated<'a>(
alternative: &Value,
declared: &BTreeMap<&'a str, Value>,
) -> Option<(&'a str, bool)> {
let properties = alternative.get("properties")?.as_object()?;
let tag = properties.get("action")?.get("const")?.as_str()?;
let (id, args) = declared.get_key_value(tag)?;
let args = shape(args);
if properties.len() == 2
&& let Some(nested) = properties.get("args")
&& shape(nested) == args
{
return Some((id, true));
}
let mut fields = undescribed(alternative);
if let Some(Value::Object(properties)) = fields.get_mut("properties") {
properties.remove("action");
}
if let Some(Value::Array(required)) = fields.get_mut("required") {
required.retain(|name| name != "action");
if required.is_empty() {
fields.as_object_mut()?.remove("required");
}
}
(shape(&fields) == args).then_some((id, false))
}
fn shape(schema: &Value) -> Value {
let mut schema = non_null(&undescribed(schema));
sort_required(&mut schema);
schema
}
fn non_null(schema: &Value) -> Value {
let Value::Object(object) = schema else {
return match schema {
Value::Array(values) => Value::Array(values.iter().map(non_null).collect()),
other => other.clone(),
};
};
if let Some(Value::Array(alternatives)) = object.get("oneOf") {
let rest: Vec<&Value> = alternatives
.iter()
.filter(|alternative| **alternative != json!({"type": "null"}))
.collect();
if object.len() == 1
&& let [only] = rest.as_slice()
{
return non_null(only);
}
}
Value::Object(
object
.iter()
.map(|(key, value)| {
let value = match (key.as_str(), value) {
("type", Value::Array(types)) => {
let types: Vec<&Value> =
types.iter().filter(|name| *name != "null").collect();
match types.as_slice() {
[only] => (*only).clone(),
_ => Value::Array(types.into_iter().cloned().collect()),
}
}
_ => non_null(value),
};
(key.clone(), value)
})
.collect(),
)
}
fn sort_required(schema: &mut Value) {
match schema {
Value::Object(object) => {
for (key, value) in object.iter_mut() {
if key == "required"
&& let Value::Array(names) = value
{
names.compact_sort_by(|a, b| a.as_str().cmp(&b.as_str()));
} else {
sort_required(value);
}
}
}
Value::Array(values) => values.iter_mut().for_each(sort_required),
_ => {}
}
}
pub(crate) fn declared_invocation<'a>(
value: &'a Value,
declared: &[&str],
) -> Option<(&'a str, Map<String, Value>)> {
let value = value.get("op").unwrap_or(value);
let id = value.get("action")?.as_str()?;
if !declared.contains(&id) {
return None;
}
let fields = if let Some(Value::Object(args)) = value.get("args") {
args.clone()
} else {
let mut fields = value.as_object()?.clone();
fields.remove("action");
fields
};
Some((id, fields))
}
fn declared_operation(id: &str, params: &[ActionParam], description: &str) -> Value {
let variant = object(vec![
("action", constant(id), true),
("args", args_schema(params), true),
]);
describe(variant, description)
}
pub fn resource_action_schema<R: crate::Resource>() -> Value {
resource_action_schema_of(crate::vocabulary::<R>(), R::seed().is_some())
}
fn resource_action_schema_of(vocabulary: &crate::Vocabulary, seeds: bool) -> Value {
let mut operations: Vec<Value> = Vec::new();
for decl in vocabulary.declarations().iter() {
operations.push(declared_operation(
decl.id,
&decl.params,
decl.description.as_deref().unwrap_or(&decl.label),
));
}
if vocabulary.edits {
operations.push(describe(
object(vec![
("action", constant(crate::FIELD_EDIT), true),
("args", json!({"type": "object"}), true),
]),
"A field edit, as the field-edit action takes it",
));
}
if seeds {
operations.push(describe(
object(vec![("action", constant("seed"), true)]),
"Write the record's first content",
));
}
object(vec![
("op", one_of(operations), true),
(
"at",
describe(
json!({"type": "integer"}),
"When the action was authored, in milliseconds since the Unix epoch: set by the host that applies it, which replaces a value given here. When content was first written elsewhere, the action that writes it takes that as its `origin`",
),
false,
),
])
}
#[must_use]
pub fn summary(schema: &Value) -> String {
let alternatives = schema
.get("oneOf")
.or_else(|| schema.get("anyOf"))
.and_then(Value::as_array)
.map_or_else(
|| vec![schema],
|alternatives| alternatives.iter().collect(),
);
alternatives
.into_iter()
.filter_map(summarise_alternative)
.collect::<Vec<_>>()
.join("; ")
}
fn summarise_alternative(schema: &Value) -> Option<String> {
if let Some(value) = schema.get("const") {
return Some(
value
.as_str()
.map_or_else(|| value.to_string(), str::to_owned),
);
}
if let Some(values) = schema.get("enum").and_then(Value::as_array) {
return Some(
values
.iter()
.map(|value| {
value
.as_str()
.map_or_else(|| value.to_string(), str::to_owned)
})
.collect::<Vec<_>>()
.join(" | "),
);
}
let properties = schema.get("properties")?.as_object()?;
if let [(name, inner)] = properties.iter().collect::<Vec<_>>().as_slice() {
let arguments = arguments_of(inner);
return Some(if arguments.is_empty() {
(*name).clone()
} else {
format!("{name} {{{arguments}}}")
});
}
Some(format!("{{{}}}", arguments_of(schema)))
}
fn arguments_of(schema: &Value) -> String {
let required: Vec<&str> = schema
.get("required")
.and_then(Value::as_array)
.map(|names| names.iter().filter_map(Value::as_str).collect())
.unwrap_or_default();
schema
.get("properties")
.and_then(Value::as_object)
.map(|properties| {
properties
.keys()
.map(|name| {
if required.contains(&name.as_str()) {
name.clone()
} else {
format!("{name}?")
}
})
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn schemas_keep_nulls_tags_and_fields_named_description;
phase optional_values_accept_null_and_keep_their_shape;
{
assert_eq!(
<Option<String>>::json_schema(),
json!({"type": ["string", "null"]})
);
let choice = nullable(one_of(vec![constant("a"), constant("b")]));
assert!(tilo_actions::validate_json_schema(&Value::Null, &choice).is_ok());
assert!(tilo_actions::validate_json_schema(&json!("a"), &choice).is_ok());
assert!(tilo_actions::validate_json_schema(&json!("c"), &choice).is_err());
const { assert!(<Option<u8>>::OPTIONAL) };
const { assert!(!<Vec<u8>>::OPTIONAL) };
}
phase internal_tags_join_the_variant_fields;
{
let schema = internal(
"kind",
"Circle",
object(vec![("radius", f64::json_schema(), true)]),
);
assert!(
tilo_actions::validate_json_schema(&json!({"kind": "Circle", "radius": 1.5}), &schema)
.is_ok()
);
assert!(tilo_actions::validate_json_schema(&json!({"radius": 1.5}), &schema).is_err());
}
phase a_copy_without_descriptions_keeps_fields_named_description;
{
let schema = describe(
object(vec![(
"description",
describe(String::json_schema(), "Text"),
true,
)]),
"A note",
);
let copy = undescribed(&schema);
assert!(copy.get("description").is_none());
assert_eq!(copy["properties"]["description"], json!({"type": "string"}));
}
}
}