Code Monkey home page Code Monkey logo

Comments (5)

edgarmueller avatar edgarmueller commented on May 20, 2024

Hi!
The validator should already report errors for all properties, e.g. when using the following schema

{
   "properties": {
     "title": { "type": "string", "minLength": 3 },
     "foo" :  { "type": "string", "pattern": "^[A-Z].*" }
   }
}

and passing {"title": "a", "foo": "a" } to the validate method it will report errors for title as well as foo:

[ {
  "schemaPath" : "#/properties/foo",
  "errors" : { },
  "msgs" : [ "a does not match pattern ^[A-Z].*" ],
  "value" : "a",
  "instancePath" : "/foo"
}, {
  "schemaPath" : "#/properties/title",
  "errors" : { },
  "msgs" : [ "a violates min length of 3" ],
  "value" : "a",
  "instancePath" : "/title"
} ]

Is this what you mean? If not, can you post an example and your expected outcome?
Thank you!

from play-json-schema-validator.

kondaurovDev avatar kondaurovDev commented on May 20, 2024

Yes, that's exactly what i mean

I've mistaken, it works:

scala> import com.eclipsesource.schema._
import com.eclipsesource.schema._

scala> import play.api.libs.json._
import play.api.libs.json._

scala> val schema = Json.fromJson[SchemaType](Json.obj("type" -> "object", "properties" -> Json.obj("name" -> Json.obj("type" -> "string"), "job" -> Json.obj("type" -> "string")), "required" -> List("name", "job")))
schema: play.api.libs.json.JsResult[com.eclipsesource.schema.SchemaType] = JsSuccess(object,)

scala> SchemaValidator.validate(schema.get, Json.obj().as[JsValue]).toString
res6: String = JsError(List((,List(ValidationError(List(Property job missing),WrappedArray({"schemaPath":"#","instancePath":"","value":{},"errors":{}})))), (,List(ValidationError(List(Property name missing),WrappedArray({"schemaPath":"#","instancePath":"","value":{},"errors":{}}))))))

There's is some bug in my code..
Thank you for reply!

from play-json-schema-validator.

kondaurovDev avatar kondaurovDev commented on May 20, 2024

Ah, no, i reproduced the problem:

scala> val result = SchemaValidator.validate(schema.get, Json.obj())
result: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = JsError(List((,List(ValidationError(List(Property job missing),WrappedArray({"schemaPath":"#","instancePath":"","value":{},"errors":{}})))), (,List(ValidationError(List(Property name missing),WrappedArray({"schemaPath":"#","instancePath":"","value":{},"errors":{}}))))))

scala> result.asEither.left.map(err => JsError.toJson(err))
res3: Product with Serializable with scala.util.Either[play.api.libs.json.JsObject,play.api.libs.json.JsObject] = Left({"obj":[{"msg":["Property name missing"],"args":[{"schemaPath":"#","instancePath":"","value":{},"errors":{}}]}]})

It fails when i try to convert JsError to JsObject with JsError.toJson method. Where is "job" property?

from play-json-schema-validator.

edgarmueller avatar edgarmueller commented on May 20, 2024

JsError.toJson currently doesn't work with the result returned by the validator since it overwrites properties with the same name during the merge (in this case the returned array contains the empty instance path twice), therefore you have to use the toJson method on the returned result like mentioned here. Let me know if that works for you.
That said, the validator should probably merge entries within the returned result, such that it is compatible with JsError.

from play-json-schema-validator.

kondaurovDev avatar kondaurovDev commented on May 20, 2024

Yes, it works!
It seems like i used it before but forgot, too much information :)
Thank you!

from play-json-schema-validator.

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.