add max-min to schemars (#4005)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-09-26 16:33:49 -07:00
committed by GitHub
parent 20777a60aa
commit 9d0f06b58f
4 changed files with 18 additions and 7 deletions

View File

@ -51912,12 +51912,16 @@
"angleEnd": {
"description": "The end angle.",
"type": "number",
"format": "double"
"format": "double",
"maximum": 360.0,
"minimum": -360.0
},
"angleStart": {
"description": "The start angle.",
"type": "number",
"format": "double"
"format": "double",
"maximum": 360.0,
"minimum": -360.0
},
"radius": {
"description": "The radius.",
@ -221804,6 +221808,8 @@
"default": null,
"type": "number",
"format": "double",
"maximum": 360.0,
"minimum": -360.0,
"nullable": true
},
"axis": {

View File

@ -23,6 +23,7 @@ use crate::{
pub struct RevolveData {
/// Angle to revolve (in degrees). Default is 360.
#[serde(default)]
#[schemars(range(min = -360.0, max = 360.0))]
pub angle: Option<f64>,
/// Axis of revolution.
pub axis: AxisOrEdgeReference,
@ -249,10 +250,12 @@ async fn inner_revolve(
args: Args,
) -> Result<Box<ExtrudeGroup>, KclError> {
if let Some(angle) = data.angle {
// Return an error if the angle is less than -360 or greater than 360.
if !(-360.0..=360.0).contains(&angle) {
// Return an error if the angle is zero.
// We don't use validate() here because we want to return a specific error message that is
// nice and we use the other data in the docs, so we still need use the derive above for the json schema.
if !(-360.0..=360.0).contains(&angle) || angle == 0.0 {
return Err(KclError::Semantic(KclErrorDetails {
message: format!("Expected angle to be between -360 and 360, found `{}`", angle),
message: format!("Expected angle to be between -360 and 360 and not 0, found `{}`", angle),
source_ranges: vec![args.source_range],
}));
}

View File

@ -1501,9 +1501,11 @@ pub enum ArcData {
AnglesAndRadius {
/// The start angle.
#[serde(rename = "angleStart", alias = "angle_start")]
#[schemars(range(min = -360.0, max = 360.0))]
angle_start: f64,
/// The end angle.
#[serde(rename = "angleEnd", alias = "angle_end")]
#[schemars(range(min = -360.0, max = 360.0))]
angle_end: f64,
/// The radius.
radius: f64,

View File

@ -951,7 +951,7 @@ async fn kcl_test_revolve_bad_angle_low() {
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([278, 314])], message: "Expected angle to be between -360 and 360, found `-455`" }"#
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([278, 314])], message: "Expected angle to be between -360 and 360 and not 0, found `-455`" }"#
);
}
@ -976,7 +976,7 @@ async fn kcl_test_revolve_bad_angle_high() {
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([278, 313])], message: "Expected angle to be between -360 and 360, found `455`" }"#
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([278, 313])], message: "Expected angle to be between -360 and 360 and not 0, found `455`" }"#
);
}