Skip to main content

dpp/data_contract/methods/validate_schema_patterns/
mod.rs

1mod v0;
2
3use crate::data_contract::DocumentName;
4use crate::prelude::DataContract;
5use crate::validation::SimpleConsensusValidationResult;
6use crate::ProtocolError;
7use platform_value::Value;
8use platform_version::version::PlatformVersion;
9use std::collections::BTreeMap;
10use v0::DefinitionsCount;
11
12impl DataContract {
13    /// Holds the regular expressions a contract's schemas carry to a bound, before anything
14    /// compiles one: compiling takes time and memory in proportion to the compiled size, not
15    /// to the pattern's length, and every pattern is compiled when a contract is parsed and
16    /// again for the validator its documents are checked with.
17    ///
18    /// A pattern is every string under a `pattern` key and every `regexPattern` of a contested
19    /// index's `fieldMatches`, wherever the document schemas and `$defs` hold one: the walk
20    /// reads keys, not the grammar, so a value a `const`, `enum` or `default` holds under one
21    /// of these keys counts too. A pattern in `$defs` counts once for each document type,
22    /// since the schema check reads every definition for each type it checks. A contract may
23    /// hold `max_contract_schema_patterns` patterns so counted, and each must compile within
24    /// `max_schema_pattern_compiled_size` bytes, a `pattern` as the document validator
25    /// compiles it and a `regexPattern` as the index parser does. A protocol version that
26    /// predates the rule refuses nothing.
27    pub fn validate_schema_patterns(
28        document_schemas: &BTreeMap<DocumentName, Value>,
29        schema_defs: Option<&BTreeMap<String, Value>>,
30        platform_version: &PlatformVersion,
31    ) -> Result<SimpleConsensusValidationResult, ProtocolError> {
32        match platform_version
33            .dpp
34            .contract_versions
35            .methods
36            .validate_schema_patterns
37        {
38            None => Ok(SimpleConsensusValidationResult::new()),
39            Some(0) => Ok(Self::validate_schema_patterns_v0(
40                document_schemas,
41                schema_defs,
42                DefinitionsCount::OncePerDocumentType,
43                false,
44                platform_version,
45            )),
46            Some(version) => Err(ProtocolError::UnknownVersionMismatch {
47                method: "DataContract::validate_schema_patterns".to_string(),
48                known_versions: vec![0],
49                received: version,
50            }),
51        }
52    }
53
54    /// What a mempool holds a contract's patterns to: the bound of
55    /// [`Self::validate_schema_patterns`], at every protocol version.
56    ///
57    /// A protocol version that predates the rule has no bound in consensus, and its blocks
58    /// must keep accepting what they accepted. Its mempool applies generation 0 of the rule
59    /// with its own system limits all the same, counting a pattern in `$defs` also once for
60    /// each `$ref` that reaches it where that is more: the JSON Schema validator those
61    /// versions check documents with compiles what a `$ref` names once for each `$ref`. That
62    /// validator resolves a `$ref` through the `$id`s of a schema, so there a contract holding
63    /// a pattern is refused with a `$ref` that is not `#/$defs/` and a definition's name, or
64    /// an `$id` that is not a plain name. It also compiles each key of a `patternProperties`,
65    /// wherever the document meta-schema leaves a schema unchecked, so that is refused there.
66    /// And it compiles each value of a property declaring `format: regex` when a document is
67    /// checked, under no bound a contract can be held to, so a schema declaring one is refused
68    /// there too. A protocol version with the rule checks such a value's syntax without
69    /// compiling it, and its mempool refuses nothing its blocks accept.
70    pub fn validate_schema_patterns_for_mempool(
71        document_schemas: &BTreeMap<DocumentName, Value>,
72        schema_defs: Option<&BTreeMap<String, Value>>,
73        platform_version: &PlatformVersion,
74    ) -> Result<SimpleConsensusValidationResult, ProtocolError> {
75        let (definitions_count, refuse_regex_format) = match platform_version
76            .dpp
77            .contract_versions
78            .methods
79            .validate_schema_patterns
80        {
81            None => (DefinitionsCount::OncePerReference, true),
82            Some(0) => (DefinitionsCount::OncePerDocumentType, false),
83            Some(version) => {
84                return Err(ProtocolError::UnknownVersionMismatch {
85                    method: "DataContract::validate_schema_patterns_for_mempool".to_string(),
86                    known_versions: vec![0],
87                    received: version,
88                })
89            }
90        };
91        Ok(Self::validate_schema_patterns_v0(
92            document_schemas,
93            schema_defs,
94            definitions_count,
95            refuse_regex_format,
96            platform_version,
97        ))
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use crate::consensus::basic::BasicError;
105    use crate::consensus::ConsensusError;
106    use crate::data_contract::errors::DataContractError;
107    use assert_matches::assert_matches;
108    use platform_value::platform_value;
109
110    /// A `pattern` and a `regexPattern` any limit admits.
111    const SMALL: &str = "^[a-z]{1,20}$";
112    /// Unicode letters, twenty times: over the limit in either dialect, though under the
113    /// 5 and 10 MiB the parsers compile a pattern within.
114    const LARGE: &str = r"^\pL{20}$";
115
116    fn before_the_rule() -> &'static PlatformVersion {
117        PlatformVersion::get(14).expect("expected protocol version 14")
118    }
119
120    /// The most patterns a contract may hold, the same at every protocol version.
121    fn most() -> usize {
122        usize::from(
123            PlatformVersion::latest()
124                .system_limits
125                .max_contract_schema_patterns,
126        )
127    }
128
129    /// The fragment of the refusal of a contract holding more than [`most`] patterns.
130    fn too_many_message() -> String {
131        format!("at most {} patterns", most())
132    }
133
134    /// A document type of `count` string properties, each with `pattern`.
135    fn schema_with_patterns(count: usize, pattern: &str) -> Value {
136        let properties: Vec<(Value, Value)> = (0..count)
137            .map(|position| {
138                (
139                    Value::Text(format!("p{position}")),
140                    platform_value!({
141                        "type": "string",
142                        "pattern": pattern,
143                        "maxLength": 63,
144                        "position": position as u64,
145                    }),
146                )
147            })
148            .collect();
149        platform_value!({
150            "type": "object",
151            "properties": Value::Map(properties),
152            "additionalProperties": false,
153        })
154    }
155
156    /// A document type whose contested index lists `count` field matches of `pattern`.
157    fn schema_with_field_matches(count: usize, pattern: &str) -> Value {
158        let field_matches: Vec<Value> = (0..count)
159            .map(|_| platform_value!({ "field": "label", "regexPattern": pattern }))
160            .collect();
161        platform_value!({
162            "type": "object",
163            "properties": {
164                "label": { "type": "string", "maxLength": 63, "position": 0 },
165            },
166            "indices": [{
167                "name": "byLabel",
168                "properties": [{ "label": "asc" }],
169                "unique": true,
170                "contested": { "fieldMatches": field_matches, "resolution": 0 },
171            }],
172            "additionalProperties": false,
173        })
174    }
175
176    fn schemas(schema: Value) -> BTreeMap<DocumentName, Value> {
177        BTreeMap::from([("item".to_string(), schema)])
178    }
179
180    fn refusal_message(result: SimpleConsensusValidationResult) -> String {
181        assert_matches!(
182            result.errors.as_slice(),
183            [ConsensusError::BasicError(BasicError::ContractError(
184                DataContractError::InvalidContractStructure(message)
185            ))] => message.clone()
186        )
187    }
188
189    fn validate(
190        document_schemas: &BTreeMap<DocumentName, Value>,
191        schema_defs: Option<&BTreeMap<String, Value>>,
192        platform_version: &PlatformVersion,
193    ) -> SimpleConsensusValidationResult {
194        DataContract::validate_schema_patterns(document_schemas, schema_defs, platform_version)
195            .expect("expected the rule to run")
196    }
197
198    fn validate_for_mempool(
199        document_schemas: &BTreeMap<DocumentName, Value>,
200        schema_defs: Option<&BTreeMap<String, Value>>,
201        platform_version: &PlatformVersion,
202    ) -> SimpleConsensusValidationResult {
203        DataContract::validate_schema_patterns_for_mempool(
204            document_schemas,
205            schema_defs,
206            platform_version,
207        )
208        .expect("expected the rule to run")
209    }
210
211    #[test]
212    fn should_hold_a_contract_to_the_most_patterns_it_may_hold() {
213        let platform_version = PlatformVersion::latest();
214        let most = usize::from(platform_version.system_limits.max_contract_schema_patterns);
215
216        let at_the_limit = schemas(schema_with_patterns(most, SMALL));
217        assert!(validate(&at_the_limit, None, platform_version).is_valid());
218
219        let over_the_limit = schemas(schema_with_patterns(most + 1, SMALL));
220        let message = refusal_message(validate(&over_the_limit, None, platform_version));
221        assert!(message.contains(&too_many_message()), "{message}");
222    }
223
224    #[test]
225    fn should_count_the_patterns_of_every_document_type_and_of_the_defs_together() {
226        let platform_version = PlatformVersion::latest();
227        let most = usize::from(platform_version.system_limits.max_contract_schema_patterns);
228        let document_schemas = BTreeMap::from([
229            ("a".to_string(), schema_with_patterns(most / 2, SMALL)),
230            (
231                "b".to_string(),
232                schema_with_field_matches(most - most / 2, SMALL),
233            ),
234        ]);
235        assert!(validate(&document_schemas, None, platform_version).is_valid());
236
237        let schema_defs = BTreeMap::from([(
238            "label".to_string(),
239            platform_value!({ "type": "string", "pattern": SMALL }),
240        )]);
241        let message = refusal_message(validate(
242            &document_schemas,
243            Some(&schema_defs),
244            platform_version,
245        ));
246        assert!(message.contains(&too_many_message()), "{message}");
247    }
248
249    #[test]
250    fn should_refuse_too_many_patterns_without_compiling_one() {
251        // None of these compiles, and the refusal is for their number
252        let document_schemas = schemas(schema_with_field_matches(most() + 1, "("));
253        let message = refusal_message(validate(&document_schemas, None, PlatformVersion::latest()));
254        assert!(message.contains(&too_many_message()), "{message}");
255    }
256
257    #[test]
258    fn should_refuse_a_pattern_that_compiles_to_more_than_the_size_limit() {
259        let platform_version = PlatformVersion::latest();
260        for document_schemas in [
261            schemas(schema_with_patterns(1, LARGE)),
262            schemas(schema_with_field_matches(1, LARGE)),
263        ] {
264            let message = refusal_message(validate(&document_schemas, None, platform_version));
265            assert!(
266                message.contains(LARGE) && message.contains("compiles within 131072 bytes"),
267                "{message}"
268            );
269        }
270        // A long pattern compiles to little: the limit is on the compiled size
271        let long = format!("^({})$", vec!["[a-z]{1,20}"; 40].join("|"));
272        assert!(validate(
273            &schemas(schema_with_patterns(1, &long)),
274            None,
275            platform_version
276        )
277        .is_valid());
278    }
279
280    #[test]
281    fn should_compile_each_pattern_as_what_reads_it_does() {
282        // The document validator reads `\w` as the ASCII word characters, the index parser as
283        // every Unicode one, which compiles to far more
284        let platform_version = PlatformVersion::latest();
285        let words = r"^\w{20}$";
286        assert!(validate(
287            &schemas(schema_with_patterns(1, words)),
288            None,
289            platform_version
290        )
291        .is_valid());
292        let message = refusal_message(validate(
293            &schemas(schema_with_field_matches(1, words)),
294            None,
295            platform_version,
296        ));
297        assert!(message.contains("`regexPattern`"), "{message}");
298    }
299
300    #[test]
301    fn should_refuse_a_pattern_that_is_not_a_regular_expression() {
302        let message = refusal_message(validate(
303            &schemas(schema_with_patterns(1, "(")),
304            None,
305            PlatformVersion::latest(),
306        ));
307        assert!(message.contains("`pattern` `(`"), "{message}");
308    }
309
310    #[test]
311    fn should_walk_a_schema_nested_deeper_than_the_stack_holds() {
312        let mut schema = platform_value!({ "pattern": "(" });
313        for _ in 0..200_000 {
314            schema = Value::Array(vec![schema]);
315        }
316        let document_schemas = schemas(schema);
317        let message = refusal_message(validate(&document_schemas, None, PlatformVersion::latest()));
318        assert!(message.contains("`pattern` `(`"), "{message}");
319        // Dropping a value this deep recurses as deep
320        std::mem::forget(document_schemas);
321    }
322
323    #[test]
324    fn should_refuse_nothing_in_consensus_before_protocol_version_15() {
325        for document_schemas in [
326            schemas(schema_with_patterns(most() + 1, SMALL)),
327            schemas(schema_with_patterns(1, LARGE)),
328            schemas(schema_with_field_matches(1, "(")),
329        ] {
330            assert!(validate(&document_schemas, None, before_the_rule()).is_valid());
331        }
332    }
333
334    #[test]
335    fn should_hold_the_mempool_to_the_bound_before_consensus_has_it() {
336        let message = refusal_message(validate_for_mempool(
337            &schemas(schema_with_patterns(most() + 1, SMALL)),
338            None,
339            before_the_rule(),
340        ));
341        assert!(message.contains(&too_many_message()), "{message}");
342
343        let message = refusal_message(validate_for_mempool(
344            &schemas(schema_with_field_matches(1, LARGE)),
345            None,
346            before_the_rule(),
347        ));
348        assert!(
349            message.contains("compiles within 131072 bytes"),
350            "{message}"
351        );
352
353        assert!(validate_for_mempool(
354            &schemas(schema_with_patterns(most(), SMALL)),
355            None,
356            before_the_rule()
357        )
358        .is_valid());
359    }
360
361    #[test]
362    fn should_keep_a_regex_format_out_of_the_mempool_only_before_the_rule() {
363        let document_schemas = schemas(platform_value!({
364            "type": "object",
365            "properties": {
366                "expression": {
367                    "type": "string",
368                    "format": "regex",
369                    "maxLength": 63,
370                    "position": 0,
371                },
372            },
373            "additionalProperties": false,
374        }));
375
376        // The JSON Schema validator compiles every value of such a property
377        let message = refusal_message(validate_for_mempool(
378            &document_schemas,
379            None,
380            before_the_rule(),
381        ));
382        assert!(message.contains("`format: regex`"), "{message}");
383        assert!(validate(&document_schemas, None, before_the_rule()).is_valid());
384
385        // The native validator checks a value's syntax without compiling it
386        let platform_version = PlatformVersion::latest();
387        assert!(validate_for_mempool(&document_schemas, None, platform_version).is_valid());
388        assert!(validate(&document_schemas, None, platform_version).is_valid());
389    }
390
391    /// `count` document types, each with one property that is a `$ref` to `definition`.
392    fn schemas_referring_to(count: usize, definition: &str) -> BTreeMap<DocumentName, Value> {
393        let reference = format!("#/$defs/{definition}");
394        (0..count)
395            .map(|index| {
396                (
397                    format!("item{index}"),
398                    platform_value!({
399                        "type": "object",
400                        "properties": {
401                            "label": { "$ref": reference.clone(), "position": 0 },
402                        },
403                        "additionalProperties": false,
404                    }),
405                )
406            })
407            .collect()
408    }
409
410    /// A definition `labels` holding `count` string properties, each with a pattern.
411    fn definitions_with_patterns(count: usize) -> BTreeMap<String, Value> {
412        BTreeMap::from([("labels".to_string(), schema_with_patterns(count, SMALL))])
413    }
414
415    #[test]
416    fn should_count_a_pattern_in_the_defs_for_each_document_type() {
417        // The schema check reads every definition for each type, referred to or not
418        let platform_version = PlatformVersion::latest();
419        let schema_defs = definitions_with_patterns(most() / 4);
420
421        let four_types = schemas_referring_to(4, "labels");
422        assert!(validate(&four_types, Some(&schema_defs), platform_version).is_valid());
423
424        let five_types = schemas_referring_to(5, "labels");
425        let message = refusal_message(validate(&five_types, Some(&schema_defs), platform_version));
426        assert!(
427            message.contains("counted for each document type"),
428            "{message}"
429        );
430
431        let mut never_referring = schemas(schema_with_patterns(0, SMALL));
432        never_referring.extend(schemas_referring_to(4, "labels"));
433        let message = refusal_message(validate(
434            &never_referring,
435            Some(&schema_defs),
436            platform_version,
437        ));
438        assert!(
439            message.contains("counted for each document type"),
440            "{message}"
441        );
442    }
443
444    #[test]
445    fn should_count_the_patterns_of_the_defs_once_in_a_contract_without_document_types() {
446        let platform_version = PlatformVersion::latest();
447        let no_document_types = BTreeMap::new();
448        assert!(validate(
449            &no_document_types,
450            Some(&definitions_with_patterns(most())),
451            platform_version
452        )
453        .is_valid());
454        let message = refusal_message(validate(
455            &no_document_types,
456            Some(&definitions_with_patterns(most() + 1)),
457            platform_version,
458        ));
459        assert!(message.contains(&too_many_message()), "{message}");
460    }
461
462    /// One document type whose `count` properties are each a `$ref` to `definition`.
463    fn schema_referring_often_to(count: usize, definition: &str) -> BTreeMap<DocumentName, Value> {
464        let reference = format!("#/$defs/{definition}");
465        let properties: Vec<(Value, Value)> = (0..count)
466            .map(|position| {
467                (
468                    Value::Text(format!("p{position}")),
469                    platform_value!({
470                        "$ref": reference.clone(),
471                        "position": position as u64,
472                    }),
473                )
474            })
475            .collect();
476        schemas(platform_value!({
477            "type": "object",
478            "properties": Value::Map(properties),
479            "additionalProperties": false,
480        }))
481    }
482
483    #[test]
484    fn should_count_a_pattern_for_each_ref_reaching_it_in_the_mempool_before_the_rule() {
485        // The JSON Schema validator compiles a definition once for each `$ref` to it; the
486        // native one, once for the type
487        let schema_defs = definitions_with_patterns(1);
488        let within = schema_referring_often_to(most(), "labels");
489        let over = schema_referring_often_to(most() + 1, "labels");
490
491        assert!(validate_for_mempool(&within, Some(&schema_defs), before_the_rule()).is_valid());
492        let message = refusal_message(validate_for_mempool(
493            &over,
494            Some(&schema_defs),
495            before_the_rule(),
496        ));
497        assert!(message.contains("for each `$ref` to it"), "{message}");
498
499        assert!(validate(&over, Some(&schema_defs), PlatformVersion::latest()).is_valid());
500    }
501
502    #[test]
503    fn should_count_a_pattern_through_the_refs_of_the_definitions_before_the_rule() {
504        // `d0` refers twice to `d1`, which refers twice to `d2`, and so on to `d7`, which
505        // holds the one pattern: 128 `$ref`s reach it
506        let mut schema_defs = definitions_with_patterns(0);
507        for level in 0..7 {
508            let next = format!("#/$defs/d{}", level + 1);
509            schema_defs.insert(
510                format!("d{level}"),
511                platform_value!({
512                    "type": "object",
513                    "properties": {
514                        "a": { "$ref": next.clone(), "position": 0 },
515                        "b": { "$ref": next, "position": 1 },
516                    },
517                    "additionalProperties": false,
518                }),
519            );
520        }
521        schema_defs.insert(
522            "d7".to_string(),
523            platform_value!({ "type": "string", "pattern": SMALL, "maxLength": 63 }),
524        );
525
526        let from_the_top = schema_referring_often_to(1, "d0");
527        let message = refusal_message(validate_for_mempool(
528            &from_the_top,
529            Some(&schema_defs),
530            before_the_rule(),
531        ));
532        assert!(message.contains("for each `$ref` to it"), "{message}");
533
534        let from_half_way = schema_referring_often_to(1, "d2");
535        assert!(
536            validate_for_mempool(&from_half_way, Some(&schema_defs), before_the_rule()).is_valid()
537        );
538    }
539
540    #[test]
541    fn should_stop_at_definitions_referring_to_each_other() {
542        let cycle = |pattern: Option<&str>| {
543            let mut leaf = platform_value!({ "type": "string", "maxLength": 63 });
544            if let Some(pattern) = pattern {
545                leaf.insert("pattern".to_string(), Value::Text(pattern.to_string()))
546                    .expect("expected a map");
547            }
548            BTreeMap::from([
549                (
550                    "a".to_string(),
551                    platform_value!({ "allOf": [{ "$ref": "#/$defs/b" }, leaf] }),
552                ),
553                (
554                    "b".to_string(),
555                    platform_value!({ "allOf": [{ "$ref": "#/$defs/a" }] }),
556                ),
557            ])
558        };
559        let document_schemas = schema_referring_often_to(1, "a");
560
561        // Nothing to compile however far the cycle is followed
562        assert!(
563            validate_for_mempool(&document_schemas, Some(&cycle(None)), before_the_rule())
564                .is_valid()
565        );
566        // A pattern compiled once more each time round
567        let message = refusal_message(validate_for_mempool(
568            &document_schemas,
569            Some(&cycle(Some(SMALL))),
570            before_the_rule(),
571        ));
572        assert!(message.contains("for each `$ref` to it"), "{message}");
573    }
574
575    #[test]
576    fn should_keep_an_id_that_may_move_a_ref_out_of_the_mempool_before_the_rule() {
577        // The JSON Schema validator looks a `$ref` up among the `$id`s of a schema before it
578        // reads it as a pointer: each `$ref` to `plain` reaches `holder`, though by its
579        // pointer it reaches no pattern
580        let holder_and_plain = |id: &str| {
581            let mut holder = schema_with_patterns(1, SMALL);
582            holder
583                .insert("$id".to_string(), Value::Text(id.to_string()))
584                .expect("expected a map");
585            BTreeMap::from([
586                ("holder".to_string(), holder),
587                ("plain".to_string(), platform_value!({ "type": "integer" })),
588            ])
589        };
590        let document_schemas = schema_referring_often_to(most() + 1, "plain");
591
592        let message = refusal_message(validate_for_mempool(
593            &document_schemas,
594            Some(&holder_and_plain("#/$defs/plain")),
595            before_the_rule(),
596        ));
597        assert!(
598            message.contains("`$id` that is not a plain name"),
599            "{message}"
600        );
601
602        // A plain name is what a keyword's data holds under this key, and moves nothing
603        assert!(validate_for_mempool(
604            &document_schemas,
605            Some(&holder_and_plain("targetId")),
606            before_the_rule()
607        )
608        .is_valid());
609
610        // Nothing to compile, wherever a `$ref` leads
611        let mut no_pattern = holder_and_plain("#/$defs/plain");
612        no_pattern.insert(
613            "holder".to_string(),
614            platform_value!({ "$id": "#/$defs/plain", "type": "string" }),
615        );
616        assert!(
617            validate_for_mempool(&document_schemas, Some(&no_pattern), before_the_rule())
618                .is_valid()
619        );
620
621        // The validation schema of protocol version 15 refuses such an `$id` itself
622        assert!(validate_for_mempool(
623            &document_schemas,
624            Some(&holder_and_plain("#/$defs/plain")),
625            PlatformVersion::latest()
626        )
627        .is_valid());
628    }
629
630    #[test]
631    fn should_keep_a_ref_that_is_no_plain_pointer_out_of_the_mempool_before_the_rule() {
632        let schema_defs = definitions_with_patterns(1);
633        for reference in [
634            // `labels`, percent-encoded, and with a tab a URL parser drops
635            "#/$defs/%6Cabels",
636            "#/$defs/lab\tels",
637            " #/$defs/labels",
638            // The document schema itself, and a part of it
639            "#",
640            "#/properties/label",
641            "#/$defs/labels/",
642            "https://example.com/schema#/$defs/labels",
643        ] {
644            let document_schemas = schemas(platform_value!({
645                "type": "object",
646                "properties": {
647                    "label": { "$ref": reference, "position": 0 },
648                },
649                "additionalProperties": false,
650            }));
651            let message = refusal_message(validate_for_mempool(
652                &document_schemas,
653                Some(&schema_defs),
654                before_the_rule(),
655            ));
656            assert!(
657                message.contains("`$ref` that is not `#/$defs/`"),
658                "{reference}: {message}"
659            );
660            assert!(
661                validate_for_mempool(&document_schemas, None, before_the_rule()).is_valid(),
662                "{reference}"
663            );
664        }
665
666        let into_a_definition = schemas(platform_value!({
667            "type": "object",
668            "properties": {
669                "label": { "$ref": "#/$defs/labels/properties/p0", "position": 0 },
670            },
671            "additionalProperties": false,
672        }));
673        assert!(
674            validate_for_mempool(&into_a_definition, Some(&schema_defs), before_the_rule())
675                .is_valid()
676        );
677    }
678
679    #[test]
680    fn should_keep_pattern_properties_out_of_the_mempool_before_the_rule() {
681        // The document meta-schema leaves a `contains` unchecked, and the JSON Schema
682        // validator compiles each key of a `patternProperties` it meets there
683        let keys = Value::Map(vec![(Value::Text(LARGE.to_string()), platform_value!({}))]);
684        let document_schemas = schemas(platform_value!({
685            "type": "object",
686            "properties": {
687                "tags": {
688                    "type": "array",
689                    "byteArray": true,
690                    "maxItems": 2,
691                    "contains": { "patternProperties": keys },
692                    "position": 0,
693                },
694            },
695            "additionalProperties": false,
696        }));
697
698        let message = refusal_message(validate_for_mempool(
699            &document_schemas,
700            None,
701            before_the_rule(),
702        ));
703        assert!(message.contains("`patternProperties`"), "{message}");
704        // A block of that protocol version accepts what it accepted
705        assert!(validate(&document_schemas, None, before_the_rule()).is_valid());
706
707        // The validation schema of protocol version 15 refuses the keyword itself, and a
708        // property may be named after it
709        let property_of_that_name = schemas(platform_value!({
710            "type": "object",
711            "properties": {
712                "patternProperties": {
713                    "type": "object",
714                    "properties": {
715                        "label": { "type": "string", "maxLength": 63, "position": 0 },
716                    },
717                    "additionalProperties": false,
718                    "position": 0,
719                },
720            },
721            "additionalProperties": false,
722        }));
723        for document_schemas in [&document_schemas, &property_of_that_name] {
724            let platform_version = PlatformVersion::latest();
725            assert!(validate_for_mempool(document_schemas, None, platform_version).is_valid());
726            assert!(validate(document_schemas, None, platform_version).is_valid());
727        }
728    }
729}