xref: /aosp_15_r20/external/json-schema-validator/doc/ecma-262.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1*78c4dd6aSAndroid Build Coastguard WorkerFor the pattern validator, we now have two options for regex in the library. The default one is `java.util.regex`; however, you can use the ECMA-262 standard library `org.jruby.joni` by configuration.
2*78c4dd6aSAndroid Build Coastguard Worker
3*78c4dd6aSAndroid Build Coastguard WorkerAs we know, the JSON schema is designed based on the Javascript language and its regex. The Java internal implementation has some differences which don't comply with the standard. For most users, these edge cases are not the issue as they are not using them anyway. Even when they are using it, they are expecting the Java regex result as the application is built on the Java platform. For users who want to ensure that they are using 100% standard patter validator, we have provided an option to override the default regex library with `org.jruby.joni` that is complying with the ECMA-262 standard.
4*78c4dd6aSAndroid Build Coastguard Worker
5*78c4dd6aSAndroid Build Coastguard Worker### Which one to choose?
6*78c4dd6aSAndroid Build Coastguard Worker
7*78c4dd6aSAndroid Build Coastguard WorkerIf you want a faster regex lib and don't care about the slight difference between Java and Javascript regex, then you don't need to do anything. The default regex lib is the `java.util.regex`.
8*78c4dd6aSAndroid Build Coastguard Worker
9*78c4dd6aSAndroid Build Coastguard WorkerIf you want to ensure full compliance, use the `org.jruby.joni`. It is 1.5 times slower then `java.util.regex`. Depending on your use case, it might not be an issue.
10*78c4dd6aSAndroid Build Coastguard Worker
11*78c4dd6aSAndroid Build Coastguard Worker### How to switch?
12*78c4dd6aSAndroid Build Coastguard Worker
13*78c4dd6aSAndroid Build Coastguard WorkerHere is the test case that shows how to pass a config object to use the ECMA-262 library.
14*78c4dd6aSAndroid Build Coastguard Worker
15*78c4dd6aSAndroid Build Coastguard Worker```java
16*78c4dd6aSAndroid Build Coastguard Worker@Test(expected = JsonSchemaException.class)
17*78c4dd6aSAndroid Build Coastguard Workerpublic void testInvalidPatternPropertiesValidatorECMA262() throws Exception {
18*78c4dd6aSAndroid Build Coastguard Worker    SchemaValidatorsConfig config = new SchemaValidatorsConfig();
19*78c4dd6aSAndroid Build Coastguard Worker    config.setEcma262Validator(true);
20*78c4dd6aSAndroid Build Coastguard Worker    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
21*78c4dd6aSAndroid Build Coastguard Worker    JsonSchema schema = factory.getSchema("{\"patternProperties\":6}", config);
22*78c4dd6aSAndroid Build Coastguard Worker
23*78c4dd6aSAndroid Build Coastguard Worker    JsonNode node = getJsonNodeFromStringContent("");
24*78c4dd6aSAndroid Build Coastguard Worker    Set<ValidationMessage> errors = schema.validate(node);
25*78c4dd6aSAndroid Build Coastguard Worker    Assert.assertEquals(errors.size(), 0);
26*78c4dd6aSAndroid Build Coastguard Worker}
27*78c4dd6aSAndroid Build Coastguard Worker```
28*78c4dd6aSAndroid Build Coastguard Worker
29*78c4dd6aSAndroid Build Coastguard Worker
30