nijikokun / generate-schema

🧞 Convert JSON Objects to MySQL, JSON Schema, Mongoose, Google BigQuery, Swagger, and more.
MIT License
1.04k stars 136 forks source link

Compatibility with Ajv ? Geojson #59

Open ghost opened 4 years ago

ghost commented 4 years ago

Context : Validate input data with Ajv

Request.Body

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               102,
               0.5
            ]
         },
         "properties":{
            "prop0":"value0"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"LineString",
            "coordinates":[
               [
                  102,
                  0
               ],
               [
                  103,
                  1
               ],
               [
                  104,
                  0
               ],
               [
                  105,
                  1
               ]
            ]
         },
         "properties":{
            "prop0":"value0",
            "prop1":0
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     100,
                     0
                  ],
                  [
                     101,
                     0
                  ],
                  [
                     101,
                     1
                  ],
                  [
                     100,
                     1
                  ],
                  [
                     100,
                     0
                  ]
               ]
            ]
         },
         "properties":{
            "prop0":"value0",
            "prop1":{
               "this":"that"
            }
         }
      }
   ]
}```

**Schema generated from the request.body** 
```json
{
   "$id":"http://json-schema.org/draft-04/schema#",
   "$schema":"http://json-schema.org/draft-04/schema#",
   "title":"Product",
   "type":"object",
   "properties":{
      "type":{
         "type":"string"
      },
      "features":{
         "type":"array",
         "items":{
            "type":"object",
            "properties":{
               "type":{
                  "type":"string"
               },
               "geometry":{
                  "type":"object",
                  "properties":{
                     "type":{
                        "type":"string"
                     },
                     "coordinates":{
                        "type":"array",
                        "items":{
                           "oneOf":[
                              {
                                 "type":"number"
                              },
                              {
                                 "type":"number"
                              },
                              {
                                 "type":"number"
                              },
                              {
                                 "type":"number"
                              },
                              {
                                 "type":"number"
                              }
                           ],
                           "type":"array"
                        }
                     }
                  }
               },
               "properties":{
                  "type":"object",
                  "properties":{
                     "prop0":{
                        "type":"string"
                     },
                     "prop1":{
                        "type":"object",
                        "properties":{
                           "this":{
                              "type":"string"
                           }
                        }
                     }
                  }
               }
            },
            "required":[
               "type",
               "geometry",
               "properties"
            ]
         }
      }
   }
}

Schema validation with Ajv

[
  {
    keyword: 'type',
    dataPath: '.features[0].geometry.coordinates[0]',
    schemaPath: '#/properties/features/items/properties/geometry/properties/coordinates/items/type',
    params: { type: 'array' },
    message: 'should be array'
  }
]

Why Ajv detects an issue ?

Thx

ghost commented 4 years ago

See link : https://gist.github.com/ThomasG77/d9c04724e63f3d327bd3ff7534bc0968