Coder Social home page Coder Social logo

Comments (3)

jimblackler avatar jimblackler commented on July 2, 2024 1

Same problem as #4.

The issue is that the library is 'BYOP'; Bring Your Own Parser. The 'data1' and 'data2' strings will need to be parsed to objects before validation.

For example
new ObjectMapper().readValue(data1, Map.class)

Rewriting your example:

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class Test {
  static String schemaStr = "{\n" +
      "  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
      "  \"properties\": {\n" +
      "    \"name\": {\n" +
      "      \"type\": \"string\",\n" +
      "      \"minLength\": 2\n" +
      "    }\n" +
      "  }\n" +
      "}";

  static String data1 = "{\n" +
      "  \"name\": \"Bill\"\n" +
      "}\n";
  static String data2 = "{\n" +
      "  \"name\": \"\"\n" +
      "}\n";

  public static void main(String[] args) {
    try {
      ObjectMapper mapper = new ObjectMapper();
      SchemaStore schemaStore = new SchemaStore(); // Initialize a SchemaStore.

      Map map = mapper.readValue(schemaStr, HashMap.class);
      Schema schema = schemaStore.loadSchema(map);
      Validator validator = new Validator();

      // Will not throw an exception.
      validator.validate(schema, new ObjectMapper().readValue(data1, Map.class));
      // Will throw a ValidationException.
      validator.validate(schema, new ObjectMapper().readValue(data2, Map.class));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I've done a bad job of making this clear, I will have a quick attempt to clarify it in the README.

The reason I've done it this way is you can make your own choice to use Jackson, GSON, org.json parser, or have data in a compatible format like JSON5 (with my usejson library) or even YAML. All these parsers have their own tradeoffs, plus you might want to integrate jsonschemafriend into an app without having a different parser forced on you. Another benefit is that you might want to validate an object before serialization; BYOP avoids an extra step.

from jsonschemafriend.

ziqiangai avatar ziqiangai commented on July 2, 2024

Thanks for your reply.
In some cases, I just want to verify that a json string meets the JSON scema standard. But the following interface makes me not know what type of parameters to pass in, and there is no description in the comments.

  public void validate(Schema schema, Object document) throws ValidationException {
    Collection<ValidationError> errors = new ArrayList<>();
    validate(schema, document, errors::add);
    if (!errors.isEmpty()) {
      throw new ListValidationException(errors);
    }
  }

I suggest adding comments to this interface to explain what type the second parameter accepts.

from jsonschemafriend.

jimblackler avatar jimblackler commented on July 2, 2024

0.11.0 now includes entry points for JSON strings (both schema and document).

See https://github.com/jimblackler/jsonschemafriend#basic-example-using-json-strings

from jsonschemafriend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.