/** * Return a default, frozen configuration * * @return a new configuration */ public static ValidationConfiguration byDefault() { return newBuilder().freeze(); }
/** * Add a library and sets it as the default * * @param uri the value for {@code $schema} * @param library the library * @return this * @see #addLibrary(String, Library) */ public ValidationConfigurationBuilder setDefaultLibrary(final String uri, final Library library) { addLibrary(uri, library); defaultLibrary = library; return this; }
/** * Return a thawed instance out of this frozen configuration * * @return a {@link ValidationConfigurationBuilder} * @see ValidationConfigurationBuilder#ValidationConfigurationBuilder(ValidationConfiguration) */ @Override public ValidationConfigurationBuilder thaw() { return new ValidationConfigurationBuilder(this); } }
/** * @param logLevel log level * @param exceptionThreshold exception threshold * @return A {@link JsonSchemaFactory} instance configured with the OpenAPI / Swagger V20 metaschema library suitable * for use in validating OpenAPI / Swagger documents */ public static JsonSchemaFactory schemaFactory(final LogLevel logLevel, final LogLevel exceptionThreshold) { return JsonSchemaFactory .newBuilder() .setValidationConfiguration( ValidationConfiguration.newBuilder() .setDefaultLibrary(OAI_V2_METASCHEMA_URI, SwaggerV20Library.get()) .setSyntaxMessages(getBundle(SwaggerV20Library.SyntaxBundle.class)) .setValidationMessages(getBundle(SwaggerV20Library.ValidationBundle.class)) .freeze()) .setReportProvider( // Only emit ERROR and above from the JSON schema validation new ListReportProvider(logLevel, exceptionThreshold)) .freeze(); }
.setDefaultLibrary("http://my.site/myschema#", library) .setValidationMessages(bundle).freeze();
@Test public void defaultLibraryIsAccountedFor() { final String ref = "x://y.z/schema#"; final Library library = Library.newBuilder().freeze(); cfg.setDefaultLibrary(ref, library); assertSame(cfg.freeze().getDefaultLibrary(), library); } }
protected TestSuite(final SchemaVersion version, final String fileName) throws IOException { final ValidationConfiguration cfg = ValidationConfiguration.newBuilder() .setDefaultVersion(version).freeze(); validator = JsonSchemaFactory.newBuilder() .setValidationConfiguration(cfg).freeze().getValidator(); testSuite = JsonLoader.fromResource("/testsuite/" + fileName + ".json"); }
@Test public void cannotPutNullValidationMessageBundle() { try { cfg.setValidationMessages(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("nullMessageBundle")); } }
@Test public void cannotPutNullSyntaxMessageBundle() { try { cfg.setSyntaxMessages(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("nullMessageBundle")); } }
@Test public void cannotPutInvalidCacheSize() { try { cfg.setCacheSize(-2); fail("No exception thrown!!"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("invalidCacheSize")); } }
/** * @return A {@link JsonSchemaFactory} instance configured with the OpenAPI / Swagger V20 metaschema library suitable * for use in validating OpenAPI / Swagger documents */ public static JsonSchemaFactory schemaFactory() { return JsonSchemaFactory .newBuilder() .setValidationConfiguration( ValidationConfiguration.newBuilder() .setDefaultLibrary(OAI_V2_METASCHEMA_URI, SwaggerV20Library.get()) .setSyntaxMessages(getBundle(SwaggerV20Library.SyntaxBundle.class)) .setValidationMessages(getBundle(SwaggerV20Library.ValidationBundle.class)) .freeze()) .setReportProvider( // Only emit ERROR and above from the JSON schema validation new ListReportProvider(LogLevel.ERROR, LogLevel.FATAL)) .freeze(); }
.setDefaultLibrary("http://my.site/myschema#", library) .setValidationMessages(bundle).freeze();
@BeforeMethod public void init() { final Keyword keyword = Keyword.newBuilder(K1) .withSyntaxChecker(mock(SyntaxChecker.class)) .withIdentityDigester(NodeType.ARRAY, NodeType.values()) .withValidatorClass(K1Validator.class) .freeze(); final Library library = DraftV4Library.get().thaw() .addKeyword(keyword).freeze(); final ValidationConfiguration cfg = ValidationConfiguration.newBuilder() .setDefaultLibrary("foo://bar#", library).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setValidationConfiguration(cfg).freeze(); processor = factory.getProcessor(); COUNT.set(0); }
/** * @param logLevel log level * @param exceptionThreshold exception threshold * @return A {@link JsonSchemaFactory} instance configured with the OpenAPI / Swagger V20 metaschema library suitable * for use in validating OpenAPI / Swagger documents */ static JsonSchemaFactory schemaFactory(final LogLevel logLevel, final LogLevel exceptionThreshold) { return JsonSchemaFactory .newBuilder() .setValidationConfiguration( ValidationConfiguration.newBuilder() .setDefaultLibrary(OAI_V2_METASCHEMA_URI, SwaggerV20Library.get()) .setSyntaxMessages(getBundle(SwaggerV20Library.SyntaxBundle.class)) .setValidationMessages(getBundle(SwaggerV20Library.ValidationBundle.class)) .freeze()) .setReportProvider( // Only emit ERROR and above from the JSON schema validation new ListReportProvider(logLevel, exceptionThreshold)) .freeze(); }
@Test public void cannotPutNullLibrary() { final String ref = "x://y.z/schema#"; try { cfg.addLibrary(ref, null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("nullLibrary")); } }
/** * Return a new thawed instance of the default configuration * * @return a new configuration builder * @see ValidationConfigurationBuilder#ValidationConfigurationBuilder() */ public static ValidationConfigurationBuilder newBuilder() { return new ValidationConfigurationBuilder(); }
@Test public void cannotOverrideExistingLibrary() { final String ref = "x://y.z/schema#"; final Library library = Library.newBuilder().freeze(); try { cfg.addLibrary(ref, library); cfg.addLibrary(ref, library); fail("No exception thrown!!"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(),BUNDLE.printf("dupLibrary", ref)); } }