1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
use crate::{
    compilation::{compile_validators, context::CompilationContext, JSONSchema, DEFAULT_SCOPE},
    content_encoding::{
        ContentEncodingCheckType, ContentEncodingConverterType,
        DEFAULT_CONTENT_ENCODING_CHECKS_AND_CONVERTERS,
    },
    content_media_type::{ContentMediaTypeCheckType, DEFAULT_CONTENT_MEDIA_TYPE_CHECKS},
    resolver::{DefaultResolver, Resolver, SchemaResolver},
    schemas, ValidationError,
};
use ahash::AHashMap;
use std::{fmt, sync::Arc};

const EXPECT_MESSAGE: &str = "Valid meta-schema!";

lazy_static::lazy_static! {
    static ref DRAFT4:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft4.json")).expect("Valid schema!");
    static ref DRAFT6:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft6.json")).expect("Valid schema!");
    static ref DRAFT7:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft7.json")).expect("Valid schema!");
    static ref DRAFT201909:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/schema.json")).expect("Valid schema!");
    static ref DRAFT201909_APPLICATOR:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/applicator.json")).expect("Valid schema!");
    static ref DRAFT201909_CONTENT:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/content.json")).expect("Valid schema!");
    static ref DRAFT201909_CORE:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/core.json")).expect("Valid schema!");
    static ref DRAFT201909_FORMAT:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/format.json")).expect("Valid schema!");
    static ref DRAFT201909_META_DATA:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/meta-data.json")).expect("Valid schema!");
    static ref DRAFT201909_VALIDATION:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2019-09/meta/validation.json")).expect("Valid schema!");
    static ref DRAFT202012:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/schema.json")).expect("Valid schema!");
    static ref DRAFT202012_CORE:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/core.json")).expect("Valid schema!");
    static ref DRAFT202012_APPLICATOR:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/applicator.json")).expect("Valid schema!");
    static ref DRAFT202012_UNEVALUATED:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/unevaluated.json")).expect("Valid schema!");
    static ref DRAFT202012_VALIDATION:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/validation.json")).expect("Valid schema!");
    static ref DRAFT202012_META_DATA:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/meta-data.json")).expect("Valid schema!");
    static ref DRAFT202012_FORMAT_ANNOTATION:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/format-annotation.json")).expect("Valid schema!");
    static ref DRAFT202012_CONTENT:serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft2020-12/meta/content.json")).expect("Valid schema!");

    static ref META_SCHEMAS: AHashMap<String, Arc<serde_json::Value>> = {
        let mut store = AHashMap::with_capacity(3);
        store.insert(
            "http://json-schema.org/draft-04/schema".to_string(),
            Arc::new(DRAFT4.clone())
        );
        store.insert(
            "http://json-schema.org/draft-06/schema".to_string(),
            Arc::new(DRAFT6.clone())
        );
        store.insert(
            "http://json-schema.org/draft-07/schema".to_string(),
            Arc::new(DRAFT7.clone())
        );
        #[cfg(feature = "draft201909")]
        {
            store.insert(
                "https://json-schema.org/draft/2019-09/schema".to_string(),
                Arc::new(DRAFT201909.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/applicator".to_string(),
                Arc::new(DRAFT201909_APPLICATOR.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/content".to_string(),
                Arc::new(DRAFT201909_CONTENT.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/core".to_string(),
                Arc::new(DRAFT201909_CORE.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/format".to_string(),
                Arc::new(DRAFT201909_FORMAT.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/meta-data".to_string(),
                Arc::new(DRAFT201909_META_DATA.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2019-09/meta/validation".to_string(),
                Arc::new(DRAFT201909_VALIDATION.clone())
            );
        }
        #[cfg(feature = "draft202012")]
        {
            store.insert(
                "https://json-schema.org/draft/2020-12/schema".to_string(),
                Arc::new(DRAFT202012.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/core".to_string(),
                Arc::new(DRAFT202012_CORE.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/applicator".to_string(),
                Arc::new(DRAFT202012_APPLICATOR.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/unevaluated".to_string(),
                Arc::new(DRAFT202012_UNEVALUATED.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/validation".to_string(),
                Arc::new(DRAFT202012_VALIDATION.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/meta-data".to_string(),
                Arc::new(DRAFT202012_META_DATA.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/format-annotation".to_string(),
                Arc::new(DRAFT202012_FORMAT_ANNOTATION.clone())
            );
            store.insert(
                "https://json-schema.org/draft/2020-12/meta/content".to_string(),
                Arc::new(DRAFT202012_CONTENT.clone())
            );
        }
        store
    };

    static ref META_SCHEMA_VALIDATORS: AHashMap<schemas::Draft, JSONSchema> = {
        let mut store = AHashMap::with_capacity(3);
        store.insert(
            schemas::Draft::Draft4,
            JSONSchema::options().without_schema_validation().compile(&DRAFT4).expect(EXPECT_MESSAGE)
        );
        store.insert(
            schemas::Draft::Draft6,
            JSONSchema::options().without_schema_validation().compile(&DRAFT6).expect(EXPECT_MESSAGE)
        );
        store.insert(
            schemas::Draft::Draft7,
            JSONSchema::options().without_schema_validation().compile(&DRAFT7).expect(EXPECT_MESSAGE)
        );
        #[cfg(feature = "draft201909")]
        store.insert(
            schemas::Draft::Draft201909,
            JSONSchema::options()
                .without_schema_validation()
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/applicator".to_string(),
                    DRAFT201909_APPLICATOR.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/content".to_string(),
                    DRAFT201909_CONTENT.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/core".to_string(),
                    DRAFT201909_CORE.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/format".to_string(),
                    DRAFT201909_FORMAT.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/meta-data".to_string(),
                    DRAFT201909_META_DATA.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2019-09/meta/validation".to_string(),
                    DRAFT201909_VALIDATION.clone()
                )
                .compile(&DRAFT201909)
                .expect(EXPECT_MESSAGE)
        );
        #[cfg(feature = "draft202012")]
        store.insert(
            schemas::Draft::Draft202012,
            JSONSchema::options()
                .without_schema_validation()
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/applicator".to_string(),
                    DRAFT202012_APPLICATOR.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/core".to_string(),
                    DRAFT202012_CORE.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/applicator".to_string(),
                    DRAFT202012_APPLICATOR.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/unevaluated".to_string(),
                    DRAFT202012_UNEVALUATED.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/validation".to_string(),
                    DRAFT202012_VALIDATION.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/meta-data".to_string(),
                    DRAFT202012_META_DATA.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/format-annotation".to_string(),
                    DRAFT202012_FORMAT_ANNOTATION.clone()
                )
                .with_document(
                    "https://json-schema.org/draft/2020-12/meta/content".to_string(),
                    DRAFT202012_CONTENT.clone()
                )
                .compile(&DRAFT202012)
                .expect(EXPECT_MESSAGE)
        );
        store
    };
}

/// Full configuration to guide the `JSONSchema` compilation.
///
/// Using a `CompilationOptions` instance you can configure the supported draft,
/// content media types and more (check the exposed methods)
#[derive(Clone)]
pub struct CompilationOptions {
    external_resolver: Arc<dyn SchemaResolver>,
    draft: Option<schemas::Draft>,
    content_media_type_checks: AHashMap<&'static str, Option<ContentMediaTypeCheckType>>,
    content_encoding_checks_and_converters:
        AHashMap<&'static str, Option<(ContentEncodingCheckType, ContentEncodingConverterType)>>,
    store: AHashMap<String, Arc<serde_json::Value>>,
    formats: AHashMap<&'static str, fn(&str) -> bool>,
    validate_formats: Option<bool>,
    validate_schema: bool,
    ignore_unknown_formats: bool,
}

impl Default for CompilationOptions {
    fn default() -> Self {
        CompilationOptions {
            external_resolver: Arc::new(DefaultResolver),
            validate_schema: true,
            draft: Option::default(),
            content_media_type_checks: AHashMap::default(),
            content_encoding_checks_and_converters: AHashMap::default(),
            store: AHashMap::default(),
            formats: AHashMap::default(),
            validate_formats: None,
            ignore_unknown_formats: true,
        }
    }
}

impl CompilationOptions {
    pub(crate) fn draft(&self) -> schemas::Draft {
        self.draft.unwrap_or_default()
    }

    /// Compile `schema` into `JSONSchema` using the currently defined options.
    pub fn compile<'a>(
        &self,
        schema: &'a serde_json::Value,
    ) -> Result<JSONSchema, ValidationError<'a>> {
        // Draft is detected in the following precedence order:
        //   - Explicitly specified;
        //   - $schema field in the document;
        //   - Draft::default()

        // Clone needed because we are going to store a Copy-on-Write (Cow) instance
        // into the final JSONSchema as well as passing `self` (the instance and not
        // the reference) would require Copy trait implementation from
        // `CompilationOptions` which is something that we would like to avoid as
        // options might contain heap-related objects (ie. an HashMap) and we want the
        // memory-related operations to be explicit
        let mut config = self.clone();
        if self.draft.is_none() {
            if let Some(draft) = schemas::draft_from_schema(schema) {
                config.with_draft(draft);
            }
        }
        let config = Arc::new(config);

        let draft = config.draft();

        let scope = match schemas::id_of(draft, schema) {
            Some(url) => url::Url::parse(url)?,
            None => DEFAULT_SCOPE.clone(),
        };
        let schema_json = Arc::new(schema.clone());
        let resolver = Arc::new(Resolver::new(
            self.external_resolver.clone(),
            draft,
            &scope,
            schema_json,
            self.store.clone(),
        )?);
        let context = CompilationContext::new(scope.into(), Arc::clone(&config), resolver);

        if self.validate_schema {
            if let Some(mut errors) = META_SCHEMA_VALIDATORS
                .get(&draft)
                .expect("Existing draft")
                .validate(schema)
                .err()
            {
                return Err(errors.next().expect("Should have at least one element"));
            }
        }

        let node = compile_validators(schema, &context)?;

        Ok(JSONSchema { node, config })
    }

    /// Ensure that the schema is going to be compiled using the defined Draft.
    ///
    /// ```rust
    /// # use jsonschema::{Draft, CompilationOptions};
    /// # let mut options = CompilationOptions::default();
    /// options.with_draft(Draft::Draft4);
    /// ```
    #[inline]
    pub fn with_draft(&mut self, draft: schemas::Draft) -> &mut Self {
        self.draft = Some(draft);
        self
    }

    pub(crate) fn content_media_type_check(
        &self,
        media_type: &str,
    ) -> Option<ContentMediaTypeCheckType> {
        if let Some(value) = self.content_media_type_checks.get(media_type) {
            *value
        } else {
            DEFAULT_CONTENT_MEDIA_TYPE_CHECKS.get(media_type).copied()
        }
    }

    /// Ensure that compiled schema is going to support the provided content media type.
    ///
    /// Arguments:
    /// * `media_type`: Name of the content media type to support (ie. "application/json")
    /// * `media_type_check`: Method checking the validity of the input string according to
    ///     the media type.
    ///     The method should return `true` if the input is valid, `false` otherwise.
    ///
    /// Example:
    /// ```rust
    /// # use jsonschema::CompilationOptions;
    /// # let mut options = CompilationOptions::default();
    /// fn check_custom_media_type(instance_string: &str) -> bool {
    ///     // In reality the check might be a bit more different ;)
    ///     instance_string != "not good"
    /// }
    /// // Add support for application/jsonschema-test
    /// options.with_content_media_type("application/jsonschema-test", check_custom_media_type);
    /// ```
    pub fn with_content_media_type(
        &mut self,
        media_type: &'static str,
        media_type_check: ContentMediaTypeCheckType,
    ) -> &mut Self {
        self.content_media_type_checks
            .insert(media_type, Some(media_type_check));
        self
    }

    /// Use a custom resolver for resolving external schema references.
    pub fn with_resolver(&mut self, resolver: impl SchemaResolver + 'static) -> &mut Self {
        self.external_resolver = Arc::new(resolver);
        self
    }

    /// Ensure that compiled schema is not supporting the provided content media type.
    ///
    /// ```rust
    /// # use jsonschema::CompilationOptions;
    /// # let mut options = CompilationOptions::default();
    /// // Disable support for application/json (which is supported by jsonschema crate)
    /// options.without_content_media_type_support("application/json");
    /// ```
    pub fn without_content_media_type_support(&mut self, media_type: &'static str) -> &mut Self {
        self.content_media_type_checks.insert(media_type, None);
        self
    }

    #[inline]
    fn content_encoding_check_and_converter(
        &self,
        content_encoding: &str,
    ) -> Option<(ContentEncodingCheckType, ContentEncodingConverterType)> {
        if let Some(value) = self
            .content_encoding_checks_and_converters
            .get(content_encoding)
        {
            *value
        } else {
            DEFAULT_CONTENT_ENCODING_CHECKS_AND_CONVERTERS
                .get(content_encoding)
                .copied()
        }
    }

    pub(crate) fn content_encoding_check(
        &self,
        content_encoding: &str,
    ) -> Option<ContentEncodingCheckType> {
        if let Some((check, _)) = self.content_encoding_check_and_converter(content_encoding) {
            Some(check)
        } else {
            None
        }
    }

    pub(crate) fn content_encoding_convert(
        &self,
        content_encoding: &str,
    ) -> Option<ContentEncodingConverterType> {
        if let Some((_, converter)) = self.content_encoding_check_and_converter(content_encoding) {
            Some(converter)
        } else {
            None
        }
    }

    /// Ensure that compiled schema is going to support the provided content encoding.
    ///
    /// Arguments:
    /// * `content_encoding`: Name of the content encoding to support (ie. "base64")
    /// * `content_encoding_check`: Method checking the validity of the input string
    ///     according to content encoding.
    ///     The method should return `true` if the input is valid, `false` otherwise.
    /// * `content_encoding_converter`: Method converting the input string into a string
    ///     representation (generally output of the decoding of the content encoding).
    ///     The method should return:
    ///     * `Err(ValidationError instance)`: in case of a `jsonschema` crate supported error (obtained via `?` or `From::from` APIs)
    ///     * `Ok(None)`: if the input string is not valid according to the content encoding
    ///     * `Ok(Some(content))`: if the input string is valid according to the content encoding, `content` will contain
    ///         the string representation of the decoded input
    ///
    /// Example:
    /// ```rust
    /// # use jsonschema::{CompilationOptions, ValidationError};
    /// # let mut options = CompilationOptions::default();
    /// // The instance_string contains a number (representing the length of the string)
    /// // a space and then the string (whose length should match the expectation).
    /// // Example: "3 The" or "4  123"
    /// fn check_custom_encoding(instance_string: &str) -> bool {
    ///     if let Some(first_space_index) = instance_string.find(' ') {
    ///         if let Ok(value) = instance_string[..first_space_index].parse::<u64>() {
    ///             return instance_string[first_space_index + 1..].chars().count() == value as usize;
    ///         }
    ///     }
    ///     false
    /// }
    /// fn converter_custom_encoding(instance_string: &str) -> Result<Option<String>, ValidationError<'static>> {
    ///     if let Some(first_space_index) = instance_string.find(' ') {
    ///         if let Ok(value) = instance_string[..first_space_index].parse::<u64>() {
    ///             if instance_string[first_space_index + 1..].chars().count() == value as usize {
    ///                 return Ok(Some(instance_string[first_space_index + 1..].to_string()));
    ///             }
    ///         }
    ///     }
    ///     Ok(None)
    /// }
    /// // Add support for prefix-length-string
    /// options.with_content_encoding("prefix-length-string", check_custom_encoding, converter_custom_encoding);
    /// ```
    pub fn with_content_encoding(
        &mut self,
        content_encoding: &'static str,
        content_encoding_check: ContentEncodingCheckType,
        content_encoding_converter: ContentEncodingConverterType,
    ) -> &mut Self {
        self.content_encoding_checks_and_converters.insert(
            content_encoding,
            Some((content_encoding_check, content_encoding_converter)),
        );
        self
    }

    /// Ensure that compiled schema is not supporting the provided content encoding.
    ///
    /// ```rust
    /// # use jsonschema::CompilationOptions;
    /// # use serde_json::Value;
    /// # let mut options = CompilationOptions::default();
    /// // Disable support for base64 (which is supported by jsonschema crate)
    /// options.without_content_encoding_support("base64");
    /// ```
    pub fn without_content_encoding_support(
        &mut self,
        content_encoding: &'static str,
    ) -> &mut Self {
        self.content_encoding_checks_and_converters
            .insert(content_encoding, None);
        self
    }

    /// Add meta schemas for supported JSON Schema drafts.
    /// It is helpful if your schema has references to JSON Schema meta-schemas:
    ///
    /// ```json
    /// {
    ///     "schema": {
    ///         "multipleOf": {
    ///             "$ref": "http://json-schema.org/draft-04/schema#/properties/multipleOf"
    ///         },
    ///         "maximum": {
    ///             "$ref": "http://json-schema.org/draft-04/schema#/properties/maximum"
    ///         }
    ///     }
    /// }
    /// ```
    ///
    /// The example above is taken from the Swagger 2.0 JSON schema.
    #[inline]
    pub fn with_meta_schemas(&mut self) -> &mut Self {
        self.store.extend(META_SCHEMAS.clone().into_iter());
        self
    }

    /// Add a new document to the store. It works as a cache to avoid making additional network
    /// calls to remote schemas via the `$ref` keyword.
    #[inline]
    pub fn with_document(&mut self, id: String, document: serde_json::Value) -> &mut Self {
        self.store.insert(id, Arc::new(document));
        self
    }
    /// Register a custom "format" validator.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use jsonschema::JSONSchema;
    /// # use serde_json::json;
    /// fn my_format(s: &str) -> bool {
    ///    // Your awesome format check!
    ///    s.ends_with("42!")
    /// }
    /// # fn foo() {
    /// let schema = json!({"type": "string", "format": "custom"});
    /// let compiled = JSONSchema::options()
    ///     .with_format("custom", my_format)
    ///     .compile(&schema)
    ///     .expect("Valid schema");
    /// // Invalid string
    /// assert!(!compiled.is_valid(&json!("foo")));
    /// // Valid string
    /// assert!(compiled.is_valid(&json!("foo42!")));
    /// # }
    /// ```
    ///
    /// The format check function should receive `&str` and return `bool`.
    pub fn with_format(&mut self, name: &'static str, format: fn(&str) -> bool) -> &mut Self {
        self.formats.insert(name, format);
        self
    }
    pub(crate) fn format(&self, format: &str) -> FormatKV<'_> {
        self.formats.get_key_value(format)
    }
    /// Do not perform schema validation during compilation.
    /// This method is only used to disable meta-schema validation for meta-schemas itself to avoid
    /// infinite recursion.
    /// The end-user will still receive `ValidationError` that are crafted manually during
    /// compilation.
    #[inline]
    pub(crate) fn without_schema_validation(&mut self) -> &mut Self {
        self.validate_schema = false;
        self
    }
    #[inline]
    /// Force enable or disable format validation.
    /// The default behavior is dependent on draft version, but the default behavior can be
    /// overridden to validate or not regardless of draft.
    pub fn should_validate_formats(&mut self, validate_formats: bool) -> &mut Self {
        self.validate_formats = Some(validate_formats);
        self
    }
    pub(crate) fn validate_formats(&self) -> bool {
        self.validate_formats
            .unwrap_or_else(|| self.draft().validate_formats_by_default())
    }

    /// Set the `false` if unrecognized formats should be reported as a validation error.
    /// By default unknown formats are silently ignored.
    pub fn should_ignore_unknown_formats(
        &mut self,
        should_ignore_unknown_formats: bool,
    ) -> &mut Self {
        self.ignore_unknown_formats = should_ignore_unknown_formats;
        self
    }

    pub(crate) const fn are_unknown_formats_ignored(&self) -> bool {
        self.ignore_unknown_formats
    }
}
// format name & a pointer to a check function
type FormatKV<'a> = Option<(&'a &'static str, &'a fn(&str) -> bool)>;

impl fmt::Debug for CompilationOptions {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("CompilationConfig")
            .field("draft", &self.draft)
            .field("content_media_type", &self.content_media_type_checks.keys())
            .field(
                "content_encoding",
                &self.content_encoding_checks_and_converters.keys(),
            )
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::CompilationOptions;
    use crate::{schemas::Draft, JSONSchema};
    use serde_json::{json, Value};
    use test_case::test_case;

    #[test_case(Some(Draft::Draft4), &json!({}) => Draft::Draft4)]
    #[test_case(None, &json!({"$schema": "http://json-schema.org/draft-06/schema#"}) => Draft::Draft6)]
    #[test_case(None, &json!({}) => Draft::default())]
    fn test_ensure_that_draft_detection_is_honored(
        draft_version_in_options: Option<Draft>,
        schema: &Value,
    ) -> Draft {
        let mut options = CompilationOptions::default();
        if let Some(draft_version) = draft_version_in_options {
            options.with_draft(draft_version);
        }
        let compiled = options.compile(schema).unwrap();
        compiled.draft()
    }

    #[test]
    fn test_with_document() {
        let schema = json!({"$ref": "http://example.json/schema.json#/rule"});
        let compiled = JSONSchema::options()
            .with_document(
                "http://example.json/schema.json".to_string(),
                json!({"rule": {"minLength": 5}}),
            )
            .compile(&schema)
            .expect("Valid schema");
        assert!(!compiled.is_valid(&json!("foo")));
        assert!(compiled.is_valid(&json!("foobar")));
    }

    fn custom(s: &str) -> bool {
        s.ends_with("42!")
    }

    #[test]
    fn custom_format() {
        let schema = json!({"type": "string", "format": "custom"});
        let compiled = JSONSchema::options()
            .with_format("custom", custom)
            .compile(&schema)
            .expect("Valid schema");
        assert!(!compiled.is_valid(&json!("foo")));
        assert!(compiled.is_valid(&json!("foo42!")));
    }
}