private ProcessingResult<ValueHolder<SchemaTree>> getResult(final JsonNode schema, final ProcessingReport report) { final ValueHolder<SchemaTree> holder = holder(schema); return ProcessingResult.uncheckedResult(processor, report, holder); }
/** * Validate a schema/instance pair * * <p>The third boolean argument instructs the validator as to whether it * should validate children even if the container (array or object) fails * to validate.</p> * * @param schema the schema * @param instance the instance * @param deepCheck see description * @return a validation report * @throws ProcessingException an exception occurred during validation * @throws NullPointerException the schema or instance is null * * @since 2.1.8 */ public ProcessingReport validate(final JsonNode schema, final JsonNode instance, final boolean deepCheck) throws ProcessingException { final ProcessingReport report = reportProvider.newReport(); final FullData data = buildData(schema, instance, deepCheck); return ProcessingResult.of(processor, report, data).getReport(); }
/** * Validate a schema/instance pair (unchecked mode) * * <p>The third boolean argument instructs the validator as to whether it * should validate children even if the container (array or object) fails * to validate.</p> * * <p>The same warnings as described in {@link * JsonSchema#validateUnchecked(JsonNode)} apply</p> * * @param schema the schema * @param instance the instance * @param deepCheck see description * @return a validation report * @throws NullPointerException the schema or instance is null * * @since 2.1.8 */ public ProcessingReport validateUnchecked(final JsonNode schema, final JsonNode instance, final boolean deepCheck) { final ProcessingReport report = reportProvider.newReport(); final FullData data = buildData(schema, instance, deepCheck); return ProcessingResult.uncheckedResult(processor, report, data) .getReport(); }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final SchemaTree tree = data.getSchema(); final JsonNode node = tree.getNode().get(keyword); FullData newData; if (node.isObject()) { newData = data.withSchema(tree.append(JsonPointer.of(keyword))); processor.process(report, newData); return; } /* * Not an object? An array */ final int size = node.size(); JsonPointer pointer; for (int index = 0; index < size; index++) { pointer = JsonPointer.of(keyword, index); newData = data.withSchema(tree.append(pointer)); processor.process(report, newData); } }
private Processor<SchemaContext, ValidatorList> buildProcessor() { final RefResolver resolver = new RefResolver(loader); final Map<JsonRef, Library> libraries = validationCfg.getLibraries(); final Library defaultLibrary = validationCfg.getDefaultLibrary(); final ValidationChain defaultChain = new ValidationChain(resolver, defaultLibrary, validationCfg); final ProcessorMap<JsonRef, SchemaContext, ValidatorList> map = new ProcessorMap<JsonRef, SchemaContext, ValidatorList>(FUNCTION); map.setDefaultProcessor(defaultChain); JsonRef ref; ValidationChain chain; for (final Map.Entry<JsonRef, Library> entry: libraries.entrySet()) { ref = entry.getKey(); chain = new ValidationChain(resolver, entry.getValue(), validationCfg); map.addEntry(ref, chain); } final Processor<SchemaContext, ValidatorList> processor = map.getProcessor(); return new CachingProcessor<SchemaContext, ValidatorList>(processor, SchemaContextEquivalence.getInstance(), validationCfg.getCacheSize()); } }
public ValidationChain(final RefResolver refResolver, final Library library, final ValidationConfiguration cfg) { final SyntaxProcessor syntaxProcessor = new SyntaxProcessor( cfg.getSyntaxMessages(), library.getSyntaxCheckers()); final ProcessorChain<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> chain1 = ProcessorChain.startWith(refResolver).chainWith(syntaxProcessor); resolver = new CachingProcessor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>( chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE, cfg.getCacheSize() ); final SchemaDigester digester = new SchemaDigester(library); final ValidatorBuilder keywordBuilder = new ValidatorBuilder(library); ProcessorChain<SchemaContext, ValidatorList> chain2 = ProcessorChain.startWith(digester).chainWith(keywordBuilder); if (cfg.getUseFormat()) { final FormatProcessor format = new FormatProcessor(library, cfg); chain2 = chain2.chainWith(format); } builder = new CachingProcessor<SchemaContext, ValidatorList>( chain2.getProcessor(), SchemaContextEquivalence.getInstance(), cfg.getCacheSize() ); }
/** * Validate a schema and return a report * * @param schema the schema * @return a report */ public ProcessingReport validateSchema(final JsonNode schema) { final ProcessingReport report = new ListProcessingReport(); return getResult(schema, report).getReport(); }
/** * Tell whether a schema is valid * * @param schema the schema * @return true if the schema is valid */ public boolean schemaIsValid(final JsonNode schema) { final ProcessingReport report = new DevNullProcessingReport(); return getResult(schema, report).isSuccess(); }
@Override public ValidatorList process(final ProcessingReport report, final SchemaContext input) throws ProcessingException { final ValueHolder<SchemaTree> in = ValueHolder.hold("schema", input.getSchema()); /* * We have to go through an intermediate report. If we re-enter this * function with a report already telling there is an error, we don't * want to wrongly report that the schema is invalid. */ final ProcessingReport r = new ListProcessingReport(report); final ValueHolder<SchemaTree> out = resolver.process(r, in); report.mergeWith(r); if (!r.isSuccess()) return null; final SchemaContext output = new SchemaContext(out.getValue(), input.getInstanceType()); return builder.process(report, output); }
/** * Constructor * * @param cfg the validation configuration to use */ public SyntaxValidator(final ValidationConfiguration cfg) { final MessageBundle syntaxMessages = cfg.getSyntaxMessages(); final ProcessorMap<JsonRef, ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> map = new ProcessorMap<JsonRef, ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>(FUNCTION); Dictionary<SyntaxChecker> dict; dict = cfg.getDefaultLibrary().getSyntaxCheckers(); final SyntaxProcessor byDefault = new SyntaxProcessor( cfg.getSyntaxMessages(), dict); map.setDefaultProcessor(byDefault); final Map<JsonRef,Library> libraries = cfg.getLibraries(); JsonRef ref; SyntaxProcessor syntaxProcessor; for (final Map.Entry<JsonRef, Library> entry: libraries.entrySet()) { ref = entry.getKey(); dict = entry.getValue().getSyntaxCheckers(); syntaxProcessor = new SyntaxProcessor(syntaxMessages, dict); map.addEntry(ref, syntaxProcessor); } processor = map.getProcessor(); }
private ProcessingReport doValidate(final JsonNode node, final boolean deepCheck) throws ProcessingException { final FullData data = new FullData(schema, new SimpleJsonTree(node), deepCheck); final ProcessingReport report = reportProvider.newReport(); final ProcessingResult<FullData> result = ProcessingResult.of(processor, report, data); return result.getReport(); }
private ProcessingReport doValidateUnchecked(final JsonNode node, final boolean deepCheck) { final FullData data = new FullData(schema, new SimpleJsonTree(node), deepCheck); final ProcessingReport report = reportProvider.newReport(); final ProcessingResult<FullData> result = ProcessingResult.uncheckedResult(processor, report, data); return result.getReport(); }
@Test(dependsOnMethods = "keywordExists") public final void exceptionOnSecondProcessingWorks() throws ProcessingException { processor = spy(new DummyProcessor(WantedState.OK, WantedState.EX, ptr1, ptr2)); try { validator.validate(processor, report, BUNDLE, data); fail("No exception thrown!!"); } catch (ProcessingException ignored) { } verify(processor, times(2)).process(anyReport(), any(FullData.class)); }
@Test(dependsOnMethods = "keywordExists") public final void OkThenOkWorks() throws ProcessingException { processor = spy(new DummyProcessor(WantedState.OK, WantedState.OK, ptr1, ptr2)); validator.validate(processor, report, BUNDLE, data); verify(processor, times(2)).process(anyReport(), any(FullData.class)); checkOkOk(report); }
@Test(dependsOnMethods = "keywordExists") public final void OkThenKoWorks() throws ProcessingException { processor = spy(new DummyProcessor(WantedState.OK, WantedState.KO, ptr1, ptr2)); validator.validate(processor, report, BUNDLE, data); verify(processor, times(2)).process(anyReport(), any(FullData.class)); checkOkKo(report); }
@Test(dependsOnMethods = "keywordExists") public final void KoThenKoWorks() throws ProcessingException { processor = spy(new DummyProcessor(WantedState.KO, WantedState.KO, ptr1, ptr2)); validator.validate(processor, report, BUNDLE, data); verify(processor, times(2)).process(anyReport(), any(FullData.class)); checkKoKo(report); }
@Test(dependsOnMethods = "keywordExists") public final void exceptionOnFirstProcessingWorks() throws ProcessingException { processor = spy(new DummyProcessor(WantedState.EX, WantedState.OK, ptr1, ptr2)); try { validator.validate(processor, report, BUNDLE, data); fail("No exception thrown!!"); } catch (ProcessingException ignored) { } verify(processor, onlyOnce()).process(anyReport(), any(FullData.class)); }
@Test public void childrenAreNotExploredByDefaultIfContainerFails() throws ProcessingException { final SchemaTree schema = new CanonicalSchemaTree(SchemaKey.anonymousKey(), RAWSCHEMA); final JsonTree instance = new SimpleJsonTree(RAWINSTANCE); final FullData data = new FullData(schema, instance); final ProcessingReport report = mock(ProcessingReport.class); processor.process(report, data); assertEquals(COUNT.get(), 0); }
@Test public void childrenAreExploredOnDemandEvenIfContainerFails() throws ProcessingException { final SchemaTree schema = new CanonicalSchemaTree(SchemaKey.anonymousKey(), RAWSCHEMA); final JsonTree instance = new SimpleJsonTree(RAWINSTANCE); final FullData data = new FullData(schema, instance, true); final ProcessingReport report = mock(ProcessingReport.class); processor.process(report, data); assertEquals(COUNT.get(), 1); }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final SchemaTree tree = data.getSchema(); final ProcessingReport subReport = new ListProcessingReport(report.getLogLevel(), LogLevel.FATAL); processor.process(subReport, data.withSchema(tree.append(PTR))); if (subReport.isSuccess()) report.error(newMsg(data, bundle, "err.draftv4.not.fail")); }