Coder Social home page Coder Social logo

Comments (6)

epoberezkin avatar epoberezkin commented on May 16, 2024

That's possible as for 10 or less items ajv generates a single expression and for more than 10 generates a "for" loop. Although there is a test for "required" it is obviously missing your case. Could you please post your schema and the data that fails?

from ajv.

m-reiniger avatar m-reiniger commented on May 16, 2024

Sure.

On a side note, this bug happens to all the schemas I created.

Schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "NodeJS Search Result Response without Hotel Infos",
    "description": "A response from a Search Result Call without hotel data",
    "type": "object",
    "properties": {
        "error": {
            "description": "error code if an error occurs",
            "type": ["integer", "null"]
        },
        "errorDesc": {
            "description": "error description",
            "type": ["string", "null"]
        },
        "status": {
            "description": "operation status",
            "type": "integer"
        },
        "statusDesc": {
            "description": "status description",
            "type": "string"
        },
        "sid": {
            "description": "search Id",
            "type": "integer"
        },
        "offers":{
            "description": "List of Offers",
            "type": "object",
            "patternProperties": {
                "^[0-9]+$": {
                    "description": "an offer",
                    "type": "object",
                    "properties": {
                        "shop": {
                            "description": "shop offer is from",
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "id": {
                                    "type": "integer"
                                }
                            },
                            "required": [
                                "name",
                                "id"
                            ]
                        },
                        "shopId": {
                            "type": "integer"
                        },
                        "bookingId": {
                            "description": "generated booking ID reflecting this offer, used later for deeplink logging",
                            "type": "string"
                        },
                        "partnerHotelId": {
                            "description": "the shops ID for this hotel",
                            "type": "string"
                        },
                        "arrival": {
                            "description": "arrival date",
                            "type": "string",
                            "pattern": "^(\\d{4})\\-(\\d{2})\\-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$"
                        },
                        "departure": {
                            "description": "departure date",
                            "type": "string",
                            "pattern": "^(\\d{4})\\-(\\d{2})\\-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$"
                        },
                        "hotelId": {
                            "description": "idealo hotel id",
                            "type": "integer"
                        },
                        "rooms": {
                            "description": "room definition",
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "roomDesc": {
                                        "type": ["string", "null"]
                                    },
                                    "roomType": {
                                        "description": "usually the number of beds",
                                        "type": ["string", "integer"]
                                    },
                                    "deeplinkOffer": {
                                        "description": "link to the hotel on the shop site",
                                        "type": "string",
                                        "format": "uri"
                                    },
                                    "priceTotal": {
                                        "type": ["number", "string"]
                                    },
                                    "priceAvg": {
                                        "type": ["number", "string"]
                                    },
                                    "partnerRoomType": {
                                        "description": "room description by the shop",
                                        "type": ["null", "string"]
                                    },
                                    "currency": {
                                        "type": "string",
                                        "pattern": "^[A-Z]{3}$"
                                    },
                                    "breakfast": {
                                        "type": "string"
                                    },
                                    "storno": {
                                        "type": "string"
                                    },
                                    "stornoText": {
                                        "type": ["string", "null"]
                                    },
                                    "creditcard": {
                                        "type": ["string", "null"]
                                    },
                                    "bookingId": {
                                        "description": "generated booking ID reflecting this offer, used later for deeplink logging",
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "creditcard",
                                    "bookingId",
                                    "roomDesc",
                                    "roomType",
                                    "deeplinkOffer",
                                    "priceTotal",
                                    "priceAvg",
                                    "partnerRoomType",
                                    "currency",
                                    "breakfast",
                                    "storno",
                                    "stornoText"
                                ]
                            },
                            "minItems": 1
                        }
                    },
                    "required": [
                        "shop",
                        "shopId",
                        "bookingId",
                        "partnerHotelId",
                        "arrival",
                        "departure",
                        "hotelId",
                        "rooms"
                    ]
                }
            },
            "additionalProperties": false
        }
    },
    "required": [
        "error",
        "errorDesc",
        "status",
        "statusDesc",
        "sid",
        "offers"
    ]
}

Data:

{
    "cachedSid": null,
    "error": null,
    "errorDesc": null,
    "sid": 808,
    "status": 250,
    "statusDesc": "Request Completed",
    "last": 0,
    "offers": {
        "0": {
            "shop": {
                "name": "",
                "id": 1
            },
            "shopId": 1,
            "bookingId": "df449f7fd1cb27005bac2d2c66f190f9",
            "partnerHotelId": "219587",
            "arrival": "2015-08-08 00:00:00",
            "departure": "2015-08-13 00:00:00",
            "hotelId": 47503,
            "rooms": [
                {
                    "roomDesc": "Economy Class Double",
                    "roomType": 2,
                    "deeplinkOffer": "http://example.com",
                    "priceTotal": "336.00",
                    "priceAvg": "67.20",
                    "partnerRoomType": "Doppelzimmer",
                    "currency": "EUR",
                    "breakfast": "unknown",
                    "storno": "yes",
                    "stornoText": null,
                    "creditcard": "unknown",
                    "bookingId": "df449f7fd1cb27005bac2d2c66f190f9"
                }
            ]
        },
        "1": {
            "shop": {
                "name": "",
                "id": 1
            },
            "shopId": 1,
            "bookingId": "81d87fb7e7acbf599c11cc4039460106",
            "partnerHotelId": "558193",
            "arrival": "2015-08-08 00:00:00",
            "departure": "2015-08-13 00:00:00",
            "hotelId": 85077,
            "rooms": [
                {
                    "roomDesc": "Standard-Doppel- oder -Zweibettzimmer - Super Sparangebot, ohne Erstattung, ohne Frühstück",
                    "roomType": 2,
                    "deeplinkOffer": "http://example.com",
                    "priceTotal": "323.00",
                    "priceAvg": "64.60",
                    "partnerRoomType": "Doppelzimmer",
                    "currency": "EUR",
                    "breakfast": "unknown",
                    "storno": "no",
                    "stornoText": null,
                    "creditcard": "unknown",
                    "bookingId": "81d87fb7e7acbf599c11cc4039460106"
                }
            ]
        },
        "2": {
            "shop": {
                "name": "",
                "id": 10
            },
            "shopId": 10,
            "bookingId": "55855141486aa48a540cb92469912ad9",
            "partnerHotelId": "219587",
            "arrival": "2015-08-08 00:00:00",
            "departure": "2015-08-13 00:00:00",
            "hotelId": 47503,
            "rooms": [
                {
                    "roomDesc": "Economy Class Double",
                    "roomType": 2,
                    "deeplinkOffer": "http://example.com",
                    "priceTotal": "336.00",
                    "priceAvg": "67.20",
                    "partnerRoomType": "Doppelzimmer",
                    "currency": "EUR",
                    "breakfast": "unknown",
                    "storno": "yes",
                    "stornoText": null,
                    "creditcard": "unknown",
                    "bookingId": "55855141486aa48a540cb92469912ad9"
                }
            ]
        }
    },
    "parserErrors": [],
    "statistic": {
        "offers": 17,
        "uniqueHotelIds": 8,
        "hotelInfos": 8,
        "shops": 19,
        "uniqueTagIds": 90,
        "tags": 90,
        "resultRequestCount": 3
    },
    "data": {
    },
    "searchFinished": true
}

This is a little test script I use:

"use strict";

var ajv = new require('ajv')({"allErrors": true}),
    fs = require('fs');


var schema = JSON.parse(fs.readFileSync('schema.json'));
var data = JSON.parse(fs.readFileSync('data.json'));


var valid = ajv.validate(schema, data);

if (!valid) {
    console.log(ajv.errors);
} else {
    console.log('JSON valid');
}

from ajv.

epoberezkin avatar epoberezkin commented on May 16, 2024

Thanks! I'll have a look

from ajv.

epoberezkin avatar epoberezkin commented on May 16, 2024

It is fixed, it was a bug with "required" validation with many properties (>10 with allErrors, > 20 without - that's why it was passing for you). Instead of testing the data on the current level, it was testing top-level data.

Your data passes too.

Thank you.

from ajv.

m-reiniger avatar m-reiniger commented on May 16, 2024

Thanks a lot.

from ajv.

epoberezkin avatar epoberezkin commented on May 16, 2024

You're welcome

from ajv.

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.