|
| 1 | +use linked_hash_map::LinkedHashMap; |
| 2 | + |
| 3 | +use crate::generation::typescript::{collapse_option, is_ts_identifier}; |
| 4 | +use crate::options::Options; |
| 5 | +use crate::shape::{self, common_shape, Shape}; |
| 6 | +use crate::util::lower_camel_case; |
| 7 | + |
| 8 | +pub struct Ctxt { |
| 9 | + options: Options, |
| 10 | + indent_level: usize, |
| 11 | +} |
| 12 | + |
| 13 | +pub type Code = String; |
| 14 | + |
| 15 | +pub fn zod_schema(name: &str, shape: &Shape, options: Options) -> Code { |
| 16 | + let mut ctxt = Ctxt { |
| 17 | + options, |
| 18 | + indent_level: 1, |
| 19 | + }; |
| 20 | + |
| 21 | + let code = type_from_shape(&mut ctxt, shape); |
| 22 | + let mut schema_name = lower_camel_case(name); |
| 23 | + schema_name.push_str("Schema"); |
| 24 | + |
| 25 | + format!("export const {} = {};\n\n", schema_name, code) |
| 26 | +} |
| 27 | + |
| 28 | +fn type_from_shape(ctxt: &mut Ctxt, shape: &Shape) -> Code { |
| 29 | + use crate::shape::Shape::*; |
| 30 | + match shape { |
| 31 | + Null | Any | Bottom => "z.unknown()".into(), |
| 32 | + Bool => "z.boolean()".into(), |
| 33 | + StringT => "z.string()".into(), |
| 34 | + Integer => "z.number()".into(), |
| 35 | + Floating => "z.number()".into(), |
| 36 | + Tuple(shapes, _n) => { |
| 37 | + let folded = shape::fold_shapes(shapes.clone()); |
| 38 | + if folded == Any && shapes.iter().any(|s| s != &Any) { |
| 39 | + generate_tuple_type(ctxt, shapes) |
| 40 | + } else { |
| 41 | + generate_vec_type(ctxt, &folded) |
| 42 | + } |
| 43 | + } |
| 44 | + VecT { elem_type: e } => generate_vec_type(ctxt, e), |
| 45 | + Struct { fields } => { |
| 46 | + if ctxt.options.infer_map_threshold.is_some_and(|lim| { fields.len() > lim }) { |
| 47 | + let inner = fields |
| 48 | + .into_iter() |
| 49 | + .map(|(_, value)| value.clone()) |
| 50 | + .fold(Shape::Bottom, common_shape); |
| 51 | + generate_map_type(ctxt, &inner) |
| 52 | + } else { |
| 53 | + generate_struct_from_field_shapes(ctxt, fields) |
| 54 | + } |
| 55 | + } |
| 56 | + MapT { val_type: v } => generate_map_type(ctxt, v), |
| 57 | + Opaque(t) => t.clone(), |
| 58 | + Optional(e) => { |
| 59 | + let inner = type_from_shape(ctxt, e); |
| 60 | + if ctxt.options.use_default_for_missing_fields { |
| 61 | + inner |
| 62 | + } else { |
| 63 | + format!("{}.optional()", inner) |
| 64 | + } |
| 65 | + }, |
| 66 | + Nullable(e) => { |
| 67 | + let inner = type_from_shape(ctxt, e); |
| 68 | + if ctxt.options.use_default_for_missing_fields { |
| 69 | + inner |
| 70 | + } else { |
| 71 | + format!("{}.nullable()", inner) |
| 72 | + } |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn generate_vec_type(ctxt: &mut Ctxt, shape: &Shape) -> Code { |
| 78 | + let inner = type_from_shape(ctxt, shape); |
| 79 | + format!("{}.array()", inner) |
| 80 | +} |
| 81 | + |
| 82 | +fn generate_map_type(ctxt: &mut Ctxt, shape: &Shape) -> Code { |
| 83 | + let (_was_optional, collapsed) = collapse_option(shape); |
| 84 | + let inner = type_from_shape(ctxt, collapsed); |
| 85 | + format!("z.record(z.string(), {})", inner) |
| 86 | +} |
| 87 | + |
| 88 | +fn generate_tuple_type(ctxt: &mut Ctxt, shapes: &[Shape]) -> Code { |
| 89 | + let mut types = Vec::new(); |
| 90 | + |
| 91 | + for shape in shapes { |
| 92 | + let typ = type_from_shape(ctxt, shape); |
| 93 | + types.push(typ); |
| 94 | + } |
| 95 | + |
| 96 | + format!("z.tuple([{}])", types.join(", ")) |
| 97 | +} |
| 98 | + |
| 99 | +fn generate_struct_from_field_shapes(ctxt: &mut Ctxt, map: &LinkedHashMap<String, Shape>) -> Code { |
| 100 | + let fields: Vec<Code> = map |
| 101 | + .iter() |
| 102 | + .map(|(name, typ)| { |
| 103 | + ctxt.indent_level += 1; |
| 104 | + let field_type = type_from_shape(ctxt, typ); |
| 105 | + ctxt.indent_level -= 1; |
| 106 | + |
| 107 | + let escape_name = !is_ts_identifier(name); |
| 108 | + |
| 109 | + format!( |
| 110 | + "{}{}{}{}: {};", |
| 111 | + " ".repeat(ctxt.indent_level), |
| 112 | + if escape_name { "\"" } else { "" }, |
| 113 | + name, |
| 114 | + if escape_name { "\"" } else { "" }, |
| 115 | + field_type |
| 116 | + ) |
| 117 | + }) |
| 118 | + .collect(); |
| 119 | + |
| 120 | + let mut code = "z.object({\n".to_string(); |
| 121 | + |
| 122 | + if !fields.is_empty() { |
| 123 | + code += &fields.join("\n"); |
| 124 | + code += "\n"; |
| 125 | + } |
| 126 | + code += &" ".repeat(ctxt.indent_level - 1); |
| 127 | + code += "})"; |
| 128 | + |
| 129 | + code |
| 130 | +} |
0 commit comments