pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
466 stars 134 forks source link

Loader callback does not find the schema-references #50

Closed cumafo closed 5 years ago

cumafo commented 5 years ago

Is there a plan for handling this?

pboettch commented 5 years ago

Can you give an simple example - but complete?

That would require a proper implementation of json_uri. Which today is a little bit simplistic.

cumafo commented 5 years ago

Thanks for quick reply. Here are schemas and a couple of json documents. Working with valijson. /C jex.tar.gz root schema is digital-receipt-schema.json

pboettch commented 5 years ago

I very quickly grepped for $ref in your schemas, which ones are the plain-name-fragment?

pboettch commented 5 years ago

Actually it seems to work fine with this library here what I did:

$ <unzip your archive>
$ <adapt the paths to make the json-schema-validate's loader find the schemas
$ path/to/json-schema-validate schema/jsonschema/digital-receipt-schema.json < json-receipts/min-sale-receipt-cash-payment.json 
loading http://api.findity.com/schema/jsonschema/business-definitions.json # 
loading http://api.findity.com/schema/jsonschema/customer-definitions.json # 
loading http://api.findity.com/schema/jsonschema/item-definitions.json # 
loading http://api.findity.com/schema/jsonschema/monetary-definitions.json # 
loading http://api.findity.com/schema/jsonschema/sales-recording-system-definitions.json # 
loading http://api.findity.com/schema/jsonschema/tender-definitions.json # 
loading http://api.findity.com/schema/jsonschema/service-common-definitions.json # 
loading http://api.findity.com/schema/jsonschema/taxi-definitions.json # 
document is valid

Could you give the error-message you are encountering?

pboettch commented 5 years ago

I tried all the instance you're provided, they are all valid. Tweaking them a little bit, I get validation errors.

cumafo commented 5 years ago

Probably something I do not understand here. What do you mean by <adapt the paths to make the json-schema-validate's loader find the schemas?

pboettch commented 5 years ago

In your archive the schemas are located in schemas-json/.

The root-schema has an $id which is: http://api.findity.com/schema/jsonschema/digital-receipt-schema.

The loader-callback (which you have to provide when using this library) of the json-schema-validate-program is called with the complete json-uri of the schema (and referenced schemas).

So it gets called with http://api.findity.com/schema/jsonschema/business-definitions.json for business-definitions.json.

The loader-callback of json-schema-validate is taking the path of the uri and tries to load the file. If it doesn't exists, it bails out.

So, I put all of your schemas to schema/jsonschema/ for the loader-callback to find it. And then it started working.

HTH.

cumafo commented 5 years ago

well, it's the loader-callback that I'm not aware of (and was looking for). Could you point me to how I define/implement that?

pboettch commented 5 years ago

Where did you look?

cumafo commented 5 years ago

while trying it out, in the code mainly. I never tried json-schema-validate. Now looking into that code and found it. Sorry about bothering you. Thanks /C

pboettch commented 5 years ago

Happy to have helped.

cumafo commented 5 years ago

One question.....: what version of your software and nlohmann did you use verifying my json? Trying your "master" and nlohmann 3.5.0 I get "no validation has succeeded but ANYOF/ONEOF them is required to validate" on different objects depending on which of the json-files I check. /C

2019-02-04 13:55:48.549 *** ./tomgwc 3.0 starting ...... 2019-02-04 13:55:48.661 loading json schemas schemas-json//business-definitions.json schemas-json//customer-definitions.json schemas-json//item-definitions.json schemas-json//monetary-definitions.json schemas-json//sales-recording-system-definitions.json schemas-json//tender-definitions.json schemas-json//service-common-definitions.json schemas-json//taxi-definitions.json .... 2019-02-04 13:56:07.922 request: POST /re-gateway/receipt HTTP/1.1 2019-02-04 13:56:07.922 Request=/re-gateway/receipt 2019-02-04 13:56:07.923 login not required, using nouser 2019-02-04 13:56:07.923 New receipt from nouser () 2019-02-04 13:56:07.923 Validating json receipt: (unverified user nouser session nosession) hash 16dd9c8182b9f11d8d2078ed4d65e4fc7f1ba628 2019-02-04 13:56:07.924 Validation failed:ERROR: '' - '{"email":"your@email.com"}': no validation has succeeded but ANYOF/ONEOF them is required to validate. 2019-02-04 13:56:07.924 { "business_unit" : { "service_id": "5922f97b-feb1-48d6-b9a6-ef76f76ebdce", "organization_number": "556838-8200", "name": "Findity store AB", "contact" : { "address": { "street_address": "Engelbrektsgatan 20", "locality": "Ludvika", "postal_code": "77187" }, "phone": "tel:+46-0771-584886" } }, "cashier" : { "id" : "5", "name" : "Anna Andersson" }, "sales_recording_system" : { "serial_number" : "EA38329", "name" : "kassa 3" }, "receipt_number" : { "sequence_number": 123456, "receipt_code" : "1281111260543493" }, "time_of_purchase" : "2018-11-09T08:49:00+01:00", "currency" : "SEK", "totals" : [ {"amount_type" : "net" , "amount" : 280.00}, {"amount_type" : "vat" , "amount" : 70.00, "taxable_amount" : 280.00, "tax_percentage": "25"}, {"amount_type" : "vat" , "amount" : 70.00}, {"amount_type" : "gross" , "amount" : 395.00}, {"amount_type" : "discount" , "amount" : 45.00}, {"amount_type" : "rounding" , "amount" : 0.00}, {"amount_type" : "paid", "amount" : 350.00} ], "text" : [ {"position" : "header", "value" : "Öppettider:\n Måndag - Fredag 10-18\n Lördag 10-15"}, {"position" : "header", "value" : "Another header text..."}, {"position" : "footer", "value" : "Sommarerbjudande:\n Köp 3 par skor få det billigaste paret på köpet."} ], "items" : [{ "type" : "sale", "identifiers" : [ {"identifier_type" : "pos", "value" : "912042"}, {"identifier_type" : "gtin", "value" : "1234567890"} ], "description" : "Skor 42 - Foder BR", "quantity" : 1, "unit" : "ea", "unit_cost_price" : 395, "discount_amount": 45, "actual_sales_unit_price" : 350, "extended_amount" : 350, "tax_percentage" : 25 }], "payments": [ { "tender": { "type": "cash" }, "amount_applied_to_bill": 350.00, "amount": 350.00 } ], "customer": { "id": { "email": "your@email.com" } }, "control_unit": { "id": "RIHTT102710007088", "code": "BGXOV2QOHHILXHLPHHXRAYZEKGFNC2BD;" }}