@Override public ValidatorList process(final ProcessingReport report, final ValidatorList input) throws ProcessingException { final SchemaContext context = input.getContext(); final JsonNode node = context.getSchema().getNode().get("format"); if (node == null) return input; final String fmt = node.textValue(); final FormatAttribute attr = attributes.get(fmt); if (attr == null) { report.warn(input.newMessage().put("domain", "validation") .put("keyword", "format") .setMessage(bundle.getMessage("warn.format.notSupported")) .putArgument("attribute", fmt)); return input; } final NodeType type = context.getInstanceType(); if (!attr.supportedTypes().contains(type)) return input; final List<KeywordValidator> validators = Lists.newArrayList(input); validators.add(formatValidator(attr)); return new ValidatorList(context, validators); }
/** * Process the input * * @param report the report to use while processing * @param input the input for this processor * @return the output * @throws ProcessingException processing failed */ @Override public ValidatorList process(final ProcessingReport report, final SchemaDigest input) throws ProcessingException { final SortedMap<String, KeywordValidator> map = Maps.newTreeMap(); String keyword; JsonNode digest; KeywordValidator validator; KeywordValidatorFactory factory; for (final Map.Entry<String, JsonNode> entry: input.getDigests().entrySet()) { keyword = entry.getKey(); digest = entry.getValue(); factory = factories.get(keyword); validator = factory.getKeywordValidator(digest); map.put(keyword, validator); } return new ValidatorList(input.getContext(), map.values()); }
final SchemaContext newContext = fullContext.getContext(); final FullData data = new FullData(newContext.getSchema(), input.getInstance(), input.isDeepCheck());
@Test public void noFormatInSchemaIsANoOp() throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, NodeType.NULL); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); final ValidatorList out = processor.process(report, in); assertTrue(Lists.newArrayList(out).isEmpty()); verifyZeroInteractions(report); }
@Test public void attributeIsBeingAskedWhatIsSupports() throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); schema.put("format", FMT); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, NodeType.NULL); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); processor.process(report, in); verify(attribute).supportedTypes(); }
@Test( dataProvider = "unsupported", dependsOnMethods = "attributeIsBeingAskedWhatIsSupports" ) public void unsupportedTypeDoesNotTriggerValidatorBuild(final JsonNode node) throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); schema.put("format", FMT); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, NodeType.getNodeType(node)); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); final ValidatorList out = processor.process(report, in); final List<KeywordValidator> validators = Lists.newArrayList(out); assertTrue(validators.isEmpty()); } }
@Test( dataProvider = "supported", dependsOnMethods = "attributeIsBeingAskedWhatIsSupports" ) public void supportedNodeTypesTriggerAttributeBuild(final JsonNode node) throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); schema.put("format", FMT); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final JsonTree instance = new SimpleJsonTree(node); final FullData data = new FullData(tree, instance); final SchemaContext context = new SchemaContext(data); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); final ValidatorList out = processor.process(report, in); final List<KeywordValidator> validators = Lists.newArrayList(out); assertEquals(validators.size(), 1); @SuppressWarnings("unchecked") final Processor<FullData, FullData> p = mock(Processor.class); validators.get(0).validate(p, report, BUNDLE, data); verify(attribute).validate(report, BUNDLE, data); }
@Test public void unknownFormatAttributesAreReportedAsWarnings() throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); schema.put("format", "foo"); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, NodeType.NULL); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); final ValidatorList out = processor.process(report, in); assertTrue(Lists.newArrayList(out).isEmpty()); verify(report).warn(captor.capture()); final ProcessingMessage message = captor.getValue(); assertMessage(message) .hasMessage(BUNDLE.printf("warn.format.notSupported", "foo")) .hasField("domain", "validation").hasField("keyword", "format") .hasField("attribute", "foo"); }