Debian Codegraph @

src/transform.rs

raw symbols SCIP
1 /*!
2 Contains the [`Transform`] trait, used to modify a constructed schema and optionally its subschemas.
3 This trait is automatically implemented for functions of the form `fn(&mut Schema) -> ()`.
4
5 # Recursive Transforms
6
7 To make a transform recursive (i.e. apply it to subschemas), you have two options:
8 1. call the [`transform_subschemas`] function within the transform function
9 2. wrap the `Transform` in a [`RecursiveTransform`]
10
11 # Examples
12
13 To add a custom property to all object schemas:
14
15 ```
16 # use schemars::{Schema, json_schema};
17 use schemars::transform::{Transform, transform_subschemas};
18
19 pub struct MyTransform;
20
21 impl Transform for MyTransform {
22 fn transform(&mut self, schema: &mut Schema) {
23 // First, make our change to this schema
24 schema.insert("my_property".to_string(), "hello world".into());
25
26 // Then apply the transform to any subschemas
27 transform_subschemas(self, schema);
28 }
29 }
30
31 let mut schema = json_schema!({
32 "type": "array",
33 "items": {}
34 });
35
36 MyTransform.transform(&mut schema);
37
38 assert_eq!(
39 schema,
40 json_schema!({
41 "type": "array",
42 "items": {
43 "my_property": "hello world"
44 },
45 "my_property": "hello world"
46 })
47 );
48 ```
49
50 The same example with a `fn` transform:
51 ```
52 # use schemars::{Schema, json_schema};
53 use schemars::transform::transform_subschemas;
54
55 fn add_property(schema: &mut Schema) {
56 schema.insert("my_property".to_string(), "hello world".into());
57
58 transform_subschemas(&mut add_property, schema)
59 }
60
61 let mut schema = json_schema!({
62 "type": "array",
63 "items": {}
64 });
65
66 add_property(&mut schema);
67
68 assert_eq!(
69 schema,
70 json_schema!({
71 "type": "array",
72 "items": {
73 "my_property": "hello world"
74 },
75 "my_property": "hello world"
76 })
77 );
78 ```
79
80 And the same example using a closure wrapped in a `RecursiveTransform`:
81 ```
82 # use schemars::{Schema, json_schema};
83 use schemars::transform::{Transform, RecursiveTransform};
84
85 let mut transform = RecursiveTransform(|schema: &mut Schema| {
86 schema.insert("my_property".to_string(), "hello world".into());
87 });
88
89 let mut schema = json_schema!({
90 "type": "array",
91 "items": {}
92 });
93
94 transform.transform(&mut schema);
95
96 assert_eq!(
97 schema,
98 json_schema!({
99 "type": "array",
100 "items": {
101 "my_property": "hello world"
102 },
103 "my_property": "hello world"
104 })
105 );
106 ```
107
108 */
109 use craterust-analyzer cargo schemars 1.2.2 crate/::_alloc_preluderust-analyzer cargo schemars 1.2.2 _alloc_prelude/::*;
110 use craterust-analyzer cargo schemars 1.2.2 crate/::{constsrust-analyzer cargo schemars 1.2.2 consts/::meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/, Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#};
111 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
112 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#;
113 use serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::{jsonrust-analyzer cargo serde_json 1.0.151 macros/json!, Maprust-analyzer cargo serde_json 1.0.151 map/Map#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#};
114
115 /// Trait used to modify a constructed schema and optionally its subschemas.
116 ///
117 /// See the [module documentation](self) for more details on implementing this trait.
118 pub trait Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# {
119 /// Applies the transform to the given [`Schema`].
120 ///
121 /// When overriding this method, you may want to call the [`transform_subschemas`] function to
122 /// also transform any subschemas.
123 fn transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#);
124
125 // Not public API
126 // Hack to enable implementing Debug on Box<dyn GenTransform> even though closures don't
127 // implement Debug
128 #[doc(hidden)]
129 fn _debug_type_namerust-analyzer cargo schemars 1.2.2 transform/Transform#_debug_type_name().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
130 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/::type_namerust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/type_name().::<Self>())
131 }
132 }
133
134 impl<F> Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for F
135 where
136 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#),
137 {
138 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[F][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
139 self(schema);
140 }
141 }
142
143 /// Applies the given [`Transform`] to all direct subschemas of the [`Schema`].
144 pub fn transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().<T: Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(t: &mut T, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
145 for (key, value) in schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().().into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][IntoIterator]into_iter().().flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().() {
146 // This is intentionally written to work with multiple JSON Schema versions, so that
147 // users can add their own transforms on the end of e.g. `SchemaSettings::draft07()` and
148 // they will still apply to all subschemas "as expected".
149 // This is why this match statement contains both `additionalProperties` (which was
150 // dropped in draft 2020-12) and `prefixItems` (which was added in draft 2020-12).
151 match key.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() {
152 "not"
153 | "if"
154 | "then"
155 | "else"
156 | "contains"
157 | "additionalProperties"
158 | "propertyNames"
159 | "additionalItems" => {
160 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
161 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
162 }
163 }
164 "allOf" | "anyOf" | "oneOf" | "prefixItems" => {
165 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(array) = value.as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().() {
166 for value in array {
167 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
168 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
169 }
170 }
171 }
172 }
173 // Support `items` array even though this is not allowed in draft 2020-12 (see above
174 // comment)
175 "items" => {
176 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(array) = value.as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().() {
177 for value in array {
178 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
179 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
180 }
181 }
182 } else if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
183 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
184 }
185 }
186 "properties" | "patternProperties" | "$defs" | "definitions" => {
187 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(obj) = value.as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().() {
188 for value in obj.values_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]values_mut().() {
189 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
190 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
191 }
192 }
193 }
194 }
195 _ => {}
196 }
197 }
198 }
199
200 // Similar to `transform_subschemas`, but only transforms subschemas that apply to the top-level
201 // object, e.g. "oneOf" but not "properties".
202 pub(craterust-analyzer cargo schemars 1.2.2 crate/) fn transform_immediate_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_immediate_subschemas().<T: Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(
203 t: &mut T,
204 schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#,
205 ) {
206 for (key, value) in schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().().into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][IntoIterator]into_iter().().flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().() {
207 match key.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() {
208 "if" | "then" | "else" => {
209 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
210 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
211 }
212 }
213 "allOf" | "anyOf" | "oneOf" => {
214 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(array) = value.as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().() {
215 for value in array {
216 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(subschema) = value.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
217 t.transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(subschema);
218 }
219 }
220 }
221 }
222 _ => {}
223 }
224 }
225 }
226
227 /// A helper struct that can wrap a non-recursive [`Transform`] (i.e. one that does not apply to
228 /// subschemas) into a recursive one.
229 ///
230 /// Its implementation of `Transform` will first apply the inner transform to the "parent" schema,
231 /// and then its subschemas (and their subschemas, and so on).
232 ///
233 /// # Example
234 /// ```
235 /// # use schemars::{Schema, json_schema};
236 /// use schemars::transform::{Transform, RecursiveTransform};
237 ///
238 /// let mut transform = RecursiveTransform(|schema: &mut Schema| {
239 /// schema.insert("my_property".to_string(), "hello world".into());
240 /// });
241 ///
242 /// let mut schema = json_schema!({
243 /// "type": "array",
244 /// "items": {}
245 /// });
246 ///
247 /// transform.transform(&mut schema);
248 ///
249 /// assert_eq!(
250 /// schema,
251 /// json_schema!({
252 /// "type": "array",
253 /// "items": {
254 /// "my_property": "hello world"
255 /// },
256 /// "my_property": "hello world"
257 /// })
258 /// );
259 /// ```
260 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
261 #[allow(clippy::exhaustive_structs)]
262 pub struct RecursiveTransformrust-analyzer cargo schemars 1.2.2 transform/RecursiveTransform#<T>(pub T);
263
264 impl<T> Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for RecursiveTransformrust-analyzer cargo schemars 1.2.2 transform/RecursiveTransform#<T>
265 where
266 T: Transformrust-analyzer cargo schemars 1.2.2 transform/Transform#,
267 {
268 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[`RecursiveTransform<T>`][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
269 self.0rust-analyzer cargo schemars 1.2.2 transform/RecursiveTransform#0..transformrust-analyzer cargo schemars 1.2.2 transform/Transform#transform().(schema);
270 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
271 }
272 }
273
274 /// Replaces boolean JSON Schemas with equivalent object schemas.
275 ///
276 /// This also applies to subschemas.
277 ///
278 /// This is useful for dialects of JSON Schema (e.g. OpenAPI 3.0) that do not support booleans as
279 /// schemas.
280 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
281 #[non_exhaustive]
282 pub struct ReplaceBoolSchemasrust-analyzer cargo schemars 1.2.2 transform/ReplaceBoolSchemas# {
283 /// When set to `true`, a schema's `additionalProperties` property will not be changed from a
284 /// boolean.
285 ///
286 /// Defaults to `false`.
287 pub skip_additional_propertiesrust-analyzer cargo schemars 1.2.2 transform/ReplaceBoolSchemas#skip_additional_properties.: bool,
288 }
289
290 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for ReplaceBoolSchemasrust-analyzer cargo schemars 1.2.2 transform/ReplaceBoolSchemas# {
291 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[ReplaceBoolSchemas][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
292 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(obj) = schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().() {
293 if self.skip_additional_propertiesrust-analyzer cargo schemars 1.2.2 transform/ReplaceBoolSchemas#skip_additional_properties. {
294 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((ap_key, ap_value)) = obj.remove_entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove_entry().("additionalProperties") {
295 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
296
297 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().(ap_key, ap_value);
298
299 return;
300 }
301 }
302
303 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
304 } else {
305 schema.ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().();
306 }
307 }
308 }
309
310 /// Restructures JSON Schema objects so that the `$ref` property will never appear alongside any
311 /// other properties.
312 ///
313 /// This also applies to subschemas.
314 ///
315 /// This is useful for versions of JSON Schema (e.g. Draft 7) that do not support other properties
316 /// alongside `$ref`.
317 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
318 #[non_exhaustive]
319 pub struct RemoveRefSiblingsrust-analyzer cargo schemars 1.2.2 transform/RemoveRefSiblings#;
320
321 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for RemoveRefSiblingsrust-analyzer cargo schemars 1.2.2 transform/RemoveRefSiblings# {
322 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[RemoveRefSiblings][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
323 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
324
325 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(obj) = schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]filter().(|o| o.lenrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1) {
326 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref_value) = obj.removerust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove().("$ref") {
327 if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(all_of) = obj.entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("allOf").or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()))
328 {
329 all_of.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!({
330 "$ref": ref_value
331 }));
332 }
333 }
334 }
335 }
336 }
337
338 /// Removes the `examples` schema property and (if present) set its first value as the `example`
339 /// property.
340 ///
341 /// This also applies to subschemas.
342 ///
343 /// This is useful for dialects of JSON Schema (e.g. OpenAPI 3.0) that do not support the `examples`
344 /// property.
345 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
346 #[non_exhaustive]
347 pub struct SetSingleExamplerust-analyzer cargo schemars 1.2.2 transform/SetSingleExample#;
348
349 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for SetSingleExamplerust-analyzer cargo schemars 1.2.2 transform/SetSingleExample# {
350 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[SetSingleExample][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
351 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
352
353 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(examples)) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("examples") {
354 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first_example) = examples.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().nextrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]next().() {
355 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("example".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), first_example);
356 }
357 }
358 }
359 }
360
361 /// Replaces the `const` schema property with a single-valued `enum` property.
362 ///
363 /// This also applies to subschemas.
364 ///
365 /// This is useful for dialects of JSON Schema (e.g. OpenAPI 3.0) that do not support the `const`
366 /// property.
367 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
368 #[non_exhaustive]
369 pub struct ReplaceConstValuerust-analyzer cargo schemars 1.2.2 transform/ReplaceConstValue#;
370
371 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for ReplaceConstValuerust-analyzer cargo schemars 1.2.2 transform/ReplaceConstValue# {
372 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[ReplaceConstValue][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
373 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
374
375 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("const") {
376 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("enum".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![value]));
377 }
378 }
379 }
380
381 /// Rename the `prefixItems` schema property to `items`.
382 ///
383 /// This also applies to subschemas.
384 ///
385 /// If the schema contains both `prefixItems` and `items`, then this additionally renames `items` to
386 /// `additionalItems`.
387 ///
388 /// This is useful for versions of JSON Schema (e.g. Draft 7) that do not support the `prefixItems`
389 /// property.
390 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
391 #[non_exhaustive]
392 pub struct ReplacePrefixItemsrust-analyzer cargo schemars 1.2.2 transform/ReplacePrefixItems#;
393
394 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for ReplacePrefixItemsrust-analyzer cargo schemars 1.2.2 transform/ReplacePrefixItems# {
395 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[ReplacePrefixItems][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
396 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
397
398 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix_items) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("prefixItems") {
399 let previous_items = schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("items".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), prefix_items);
400
401 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(previous_items) = previous_items {
402 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("additionalItems".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), previous_items);
403 }
404 }
405 }
406 }
407
408 /// Adds a `"nullable": true` property to schemas that allow `null` types.
409 ///
410 /// This also applies to subschemas.
411 ///
412 /// This is useful for dialects of JSON Schema (e.g. OpenAPI 3.0) that use `nullable` instead of
413 /// explicit null types.
414 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
415 #[non_exhaustive]
416 pub struct AddNullablerust-analyzer cargo schemars 1.2.2 transform/AddNullable# {
417 /// When set to `true` (the default), `"null"` will also be removed from the schemas `type`.
418 pub remove_null_typerust-analyzer cargo schemars 1.2.2 transform/AddNullable#remove_null_type.: bool,
419 /// When set to `true` (the default), a schema that has a type only allowing `null` will also
420 /// have the equivalent `"const": null` inserted.
421 pub add_const_nullrust-analyzer cargo schemars 1.2.2 transform/AddNullable#add_const_null.: bool,
422 }
423
424 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for AddNullablerust-analyzer cargo schemars 1.2.2 transform/AddNullable# {
425 fn defaultrust-analyzer cargo schemars 1.2.2 transform/impl#[AddNullable][Default]default().() -> Selfrust-analyzer cargo schemars 1.2.2 transform/impl#[AddNullable][Default] {
426 Selfrust-analyzer cargo schemars 1.2.2 transform/impl#[AddNullable][Default] {
427 remove_null_typerust-analyzer cargo schemars 1.2.2 transform/AddNullable#remove_null_type.: true,
428 add_const_nullrust-analyzer cargo schemars 1.2.2 transform/AddNullable#add_const_null.: true,
429 }
430 }
431 }
432
433 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for AddNullablerust-analyzer cargo schemars 1.2.2 transform/AddNullable# {
434 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[AddNullable][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
435 if schema.has_typerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]has_type().("null") {
436 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("nullable".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), true.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
437
438 // has_type returned true so we know "type" exists and is a string or array
439 let ty = schema.get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().("type").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
440 let only_allows_null =
441 ty.is_stringrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]is_string().() || ty.as_arrayrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|v| v rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."null");
442
443 if only_allows_null {
444 if self.add_const_nullrust-analyzer cargo schemars 1.2.2 transform/AddNullable#add_const_null. {
445 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("const".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null#);
446
447 if self.remove_null_typerust-analyzer cargo schemars 1.2.2 transform/AddNullable#remove_null_type. {
448 schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("type");
449 }
450 } else if self.remove_null_typerust-analyzer cargo schemars 1.2.2 transform/AddNullable#remove_null_type. {
451 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().ty = Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().());
452 }
453 } else if self.remove_null_typerust-analyzer cargo schemars 1.2.2 transform/AddNullable#remove_null_type. {
454 // We know `type` is an array containing at least one non-null type
455 let array = ty.as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
456 array.retainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]retain().(|t| t rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."null");
457
458 if array.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 {
459 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().ty = array.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(0);
460 }
461 }
462 }
463
464 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
465 }
466 }
467
468 /// Replaces the `unevaluatedProperties` schema property with the `additionalProperties` property,
469 /// adding properties from a schema's subschemas to its `properties` where necessary.
470 ///
471 /// This also applies to subschemas.
472 ///
473 /// This is useful for versions of JSON Schema (e.g. Draft 7) that do not support the
474 /// `unevaluatedProperties` property.
475 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
476 #[non_exhaustive]
477 pub struct ReplaceUnevaluatedPropertiesrust-analyzer cargo schemars 1.2.2 transform/ReplaceUnevaluatedProperties#;
478
479 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for ReplaceUnevaluatedPropertiesrust-analyzer cargo schemars 1.2.2 transform/ReplaceUnevaluatedProperties# {
480 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[ReplaceUnevaluatedProperties][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
481 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
482
483 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(up) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("unevaluatedProperties") else {
484 return;
485 };
486
487 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("additionalProperties".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), up);
488
489 let mut gather_property_names = GatherPropertyNamesrust-analyzer cargo schemars 1.2.2 transform/GatherPropertyNames#::defaultrust-analyzer cargo schemars 1.2.2 transform/impl#[GatherPropertyNames][Default]default().();
490 gather_property_names.transformrust-analyzer cargo schemars 1.2.2 transform/impl#[GatherPropertyNames][Transform]transform().(schema);
491 let property_names = gather_property_names.0rust-analyzer cargo schemars 1.2.2 transform/GatherPropertyNames#0.;
492
493 if property_names.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`]is_empty().() {
494 return;
495 }
496
497 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(properties) = schema
498 .ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().()
499 .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("properties")
500 .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(Maprust-analyzer cargo serde_json 1.0.151 map/Map#::newrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]new().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
501 .as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().()
502 {
503 for name in property_names {
504 properties.entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().(name).or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(true.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
505 }
506 }
507 }
508 }
509
510 // Helper for getting property names for all *immediate* subschemas
511 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
512 struct GatherPropertyNamesrust-analyzer cargo schemars 1.2.2 transform/GatherPropertyNames#(BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>);
513
514 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for GatherPropertyNamesrust-analyzer cargo schemars 1.2.2 transform/GatherPropertyNames# {
515 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[GatherPropertyNames][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
516 self.0rust-analyzer cargo schemars 1.2.2 transform/GatherPropertyNames#0..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`][`Extend<T>`]extend().(
517 schema
518 .as_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object().()
519 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]iter().()
520 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|o| o.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().("properties"))
521 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_objectrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object().)
522 .flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(Maprust-analyzer cargo serde_json 1.0.151 map/Map#::keysrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]keys().)
523 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().(),
524 );
525
526 transform_immediate_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_immediate_subschemas().(self, schema);
527 }
528 }
529
530 /// Removes any `format` values that are not defined by the JSON Schema standard or explicitly
531 /// allowed by a custom list.
532 ///
533 /// This also applies to subschemas.
534 ///
535 /// By default, this will infer the version of JSON Schema from the schema's `$schema` property,
536 /// and no additional formats will be allowed (even when the JSON schema allows nonstandard
537 /// formats).
538 ///
539 /// # Example
540 /// ```
541 /// use schemars::json_schema;
542 /// use schemars::transform::{RestrictFormats, Transform};
543 ///
544 /// let mut schema = schemars::json_schema!({
545 /// "$schema": "https://json-schema.org/draft/2020-12/schema",
546 /// "anyOf": [
547 /// {
548 /// "type": "string",
549 /// "format": "uuid"
550 /// },
551 /// {
552 /// "$schema": "http://json-schema.org/draft-07/schema#",
553 /// "type": "string",
554 /// "format": "uuid"
555 /// },
556 /// {
557 /// "type": "string",
558 /// "format": "allowed-custom-format"
559 /// },
560 /// {
561 /// "type": "string",
562 /// "format": "forbidden-custom-format"
563 /// }
564 /// ]
565 /// });
566 ///
567 /// let mut transform = RestrictFormats::default();
568 /// transform.allowed_formats.insert("allowed-custom-format".into());
569 /// transform.transform(&mut schema);
570 ///
571 /// assert_eq!(
572 /// schema,
573 /// json_schema!({
574 /// "$schema": "https://json-schema.org/draft/2020-12/schema",
575 /// "anyOf": [
576 /// {
577 /// // "uuid" format is defined in draft 2020-12.
578 /// "type": "string",
579 /// "format": "uuid"
580 /// },
581 /// {
582 /// // "uuid" format is not defined in draft-07, so is removed from this subschema.
583 /// "$schema": "http://json-schema.org/draft-07/schema#",
584 /// "type": "string"
585 /// },
586 /// {
587 /// // "allowed-custom-format" format was present in `allowed_formats`...
588 /// "type": "string",
589 /// "format": "allowed-custom-format"
590 /// },
591 /// {
592 /// // ...but "forbidden-custom-format" format was not, so is also removed.
593 /// "type": "string"
594 /// }
595 /// ]
596 /// })
597 /// );
598 /// ```
599 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
600 #[non_exhaustive]
601 pub struct RestrictFormatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats# {
602 /// Whether to read the schema's `$schema` property to determine which version of JSON Schema
603 /// is being used, and allow only formats defined in that standard. If this is `true` but the
604 /// JSON Schema version can't be determined because `$schema` is missing or unknown, then no
605 /// `format` values will be removed.
606 ///
607 /// If this is set to `false`, then only the formats explicitly included in
608 /// [`allowed_formats`](Self::allowed_formats) will be allowed.
609 ///
610 /// By default, this is `true`.
611 pub infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#infer_from_meta_schema.: bool,
612 /// Values of the `format` property in schemas that will always be allowed, regardless of the
613 /// inferred version of JSON Schema.
614 pub allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#allowed_formats.: BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str>>,
615 }
616
617 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for RestrictFormatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats# {
618 fn defaultrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Default]default().() -> Selfrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Default] {
619 Selfrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Default] {
620 infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#infer_from_meta_schema.: true,
621 allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#allowed_formats.: BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T>`]new().(),
622 }
623 }
624 }
625
626 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for RestrictFormatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats# {
627 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
628 let mut implementation = RestrictFormatsImplrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl# {
629 infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#infer_from_meta_schema.: self.infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#infer_from_meta_schema.,
630 inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
631 allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#allowed_formats.: &self.allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#allowed_formats.,
632 };
633
634 implementation.transformrust-analyzer cargo schemars 1.2.2 transform/impl#[`RestrictFormatsImpl<'_>`][Transform]transform().(schema);
635 }
636 }
637
638 static DEFINED_FORMATSrust-analyzer cargo schemars 1.2.2 transform/DEFINED_FORMATS.: &[&str] = &[
639 // `duration` and `uuid` are defined only in draft 2019-09+
640 "duration",
641 "uuid",
642 // The rest are also defined in draft-07:
643 "date-time",
644 "date",
645 "time",
646 "email",
647 "idn-email",
648 "hostname",
649 "idn-hostname",
650 "ipv4",
651 "ipv6",
652 "uri",
653 "uri-reference",
654 "iri",
655 "iri-reference",
656 "uri-template",
657 "json-pointer",
658 "relative-json-pointer",
659 "regex",
660 ];
661
662 struct RestrictFormatsImplrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#<'a> {
663 infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#infer_from_meta_schema.: bool,
664 inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'static [&'static str]>,
665 allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#allowed_formats.: &'a BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str>>,
666 }
667
668 impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for RestrictFormatsImplrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#<'_> {
669 fn transformrust-analyzer cargo schemars 1.2.2 transform/impl#[`RestrictFormatsImpl<'_>`][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) {
670 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(obj) = schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().() else {
671 return;
672 };
673
674 let previous_inferred_formats = self.inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats.;
675
676 if self.infer_from_meta_schemarust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#infer_from_meta_schema. && obj.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().("$schema") {
677 self.inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats. = match obj
678 .getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().("$schema")
679 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_strrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_str().)
680 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().()
681 {
682 meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07. => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&DEFINED_FORMATSrust-analyzer cargo schemars 1.2.2 transform/DEFINED_FORMATS.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().),
683 meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT2019_09rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT2019_09. | meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT2020_12rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT2020_12. => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(DEFINED_FORMATSrust-analyzer cargo schemars 1.2.2 transform/DEFINED_FORMATS.),
684 _ => {
685 // we can't handle an unrecognised meta-schema
686 return;
687 }
688 };
689 }
690
691 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(format) = obj.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().("format").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_strrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_str().) {
692 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#allowed_formats..containsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`]contains().(format)
693 && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self
694 .inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats.
695 .is_some_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some_and().(|formats| formats.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&format))
696 {
697 obj.removerust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove().("format");
698 }
699 }
700
701 transform_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_subschemas().(self, schema);
702
703 self.inferred_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormatsImpl#inferred_formats. = previous_inferred_formats;
704 }
705 }
706
707 #[cfg(test)]
708 mod testsrust-analyzer cargo schemars 1.2.2 transform/tests/ {
709 use superrust-analyzer cargo schemars 1.2.2 transform/::*;
710 use pretty_assertionsrust-analyzer cargo pretty_assertions 1.4.1 crate/::assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!;
711
712 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
713 fn restrict_formatsrust-analyzer cargo schemars 1.2.2 transform/tests/restrict_formats().() {
714 let mut schema = json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({
715 "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT2020_12rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT2020_12.,
716 "anyOf": [
717 { "format": "uuid" },
718 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "uuid" },
719 { "$schema": "http://unknown", "format": "uuid" },
720 { "format": "date" },
721 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "date" },
722 { "$schema": "http://unknown", "format": "date" },
723 { "format": "custom1" },
724 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "custom1" },
725 { "$schema": "http://unknown", "format": "custom1" },
726 { "format": "custom2" },
727 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "custom2" },
728 { "$schema": "http://unknown", "format": "custom2" },
729 ]
730 });
731
732 let mut transform = RestrictFormatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#::defaultrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Default]default().();
733 transform.allowed_formatsrust-analyzer cargo schemars 1.2.2 transform/RestrictFormats#allowed_formats..insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`]insert().("custom1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
734 transform.transformrust-analyzer cargo schemars 1.2.2 transform/impl#[RestrictFormats][Transform]transform().(&mut schema);
735
736 assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!(
737 schema,
738 json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({
739 "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT2020_12rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT2020_12.,
740 "anyOf": [
741 { "format": "uuid" },
742 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07. },
743 { "$schema": "http://unknown", "format": "uuid" },
744 { "format": "date" },
745 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "date" },
746 { "$schema": "http://unknown", "format": "date" },
747 { "format": "custom1" },
748 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07., "format": "custom1" },
749 { "$schema": "http://unknown", "format": "custom1" },
750 { },
751 { "$schema": meta_schemasrust-analyzer cargo schemars 1.2.2 consts/meta_schemas/::DRAFT07rust-analyzer cargo schemars 1.2.2 consts/meta_schemas/DRAFT07. },
752 { "$schema": "http://unknown", "format": "custom2" },
753 ]
754 })
755 );
756 }
757 }