Joi: I have one question can Joi validate like this

Created on 26 Nov 2015  路  19Comments  路  Source: sideway/joi

currentLocation: Joi.object().keys({
                        type: Joi.string().required().valid(["Point"]),
                        coordinates: [{
                            longitude: Joi.number().precision(8),
                            latitude: Joi.number().precision(8)
                        }]

if i am wrong can you please suggest me the write way to write valid format

support

Most helpful comment

Unless you want to deliberately exclude half the planet you probably want:

Joi.array().ordered([
  Joi.number().min(-90).max(90).required(),
  Joi.number().min(-180).max(180).required()
])

(i.e. allow Southern Hemisphere latitudes down to -90).
Other than that this is a pretty elegant way of doing it @Marsup

All 19 comments

I don't even know what you're trying to do, what you already tried, why it failed.

Hey @Marsup I just want to get the input from user for this schema

currentLocation: {
        'type': {type: String, enum: "Point", default: "Point"},
        coordinates: {type: [Number], default: [0, 0]}
    },

i just want to this in database
Please suggest me any solution

You only answered 1 question out of 3.

@Marsup
I am not getting you

Ooh

  1. what you're trying to do
    I am trying to save user location coordinates in database.
  2. what you already tried
    I make currentLocation as array object
currentLocation: Joi.object().keys({
                        type: Joi.string().required().valid(["Point"]),
                        coordinates: [{
                            longitude: Joi.number().precision(8),
                            latitude: Joi.number().precision(8)
                        }]

3.why it failed

i really no idea why it failed but Joi prompt error
currentLocation must be an object"

What have you already tried and why did it fail your expectations ?

I am also trying like this

 currentLocation: Joi.object().keys({
                        type: Joi.string().required().valid(["Point"]),
                        coordinates: {
                            longitude: Joi.number().precision(8),
                            latitude: Joi.number().precision(8)
                        }

                    }).description('Please use this format [ longitude, latitude]'),

I still don't see your problem.

@Marsup Okay No problem so please can you tell me that this is write way of Joi Validation
I think it help you what's going on. Sorry for you taking time

When is put the fields values in swagger
like this

{type:Point,coordinates:{longitude:30,latitude:30}}

index

I don't know swagger enough but it seems wrong, please use a better tool to make your tests.
Also joi has no ties with http, please report issues with plain javascript objects.

can you suggest me better tool for API's documentation tool

For documentation or making queries ? To do queries, basically anything else, curl, browser extensions, ...

Thank you @Marsup

@Marsup he's trying to store location coordinates in database. Fine! But the problem: How shall he write a Joi validation to validate the coordinates? The standard format is: [lat, lng] array.

His problem was probably different as it failed on the object, but I never had enough clues to give an answer.

For yours, well, if I translate the constraints of those right, it's :

Joi.array().ordered([
  Joi.number().min(0).max(90).required(),
  Joi.number().min(-180).max(180).required()
])

This is the basics, you might want to add other constraints on top of it.

Yeah, you're right! This is what was expected :)

I had it this way btw:

Joi.array().items(Joi.number()).min(2).max(2)

But I guess, your solution is more elegant! Sound's good @Marsup :+1:

Joi.array().ordered([
  Joi.number().min(0).max(90).required(),
  Joi.number().min(-180).max(180).required()
])

Unless you want to deliberately exclude half the planet you probably want:

Joi.array().ordered([
  Joi.number().min(-90).max(90).required(),
  Joi.number().min(-180).max(180).required()
])

(i.e. allow Southern Hemisphere latitudes down to -90).
Other than that this is a pretty elegant way of doing it @Marsup

I am trying to validate an amount using the following:
Joi.number().min(0).precision(2)
but it still allows numbers like 23.566 and stores them in mongo db.

Faced the same issue, May be helpful for somebody else.

Below is an example GeoJSON point representing the approximate location of San Francisco. Note that longitude comes first in a GeoJSON coordinate array, not latitude.

{
  "type" : "Point",
  "coordinates" : [
    -122.5,
    37.7
  ]
}

const pointSchema = new mongoose.Schema({
  type: {
    type: String,
    enum: ["Point"],
    required: true
  },
  coordinates: {
    type: [Number],
    required: true
  }
});

//mongo schema
location: {
    type: pointSchema,
    required: true
  },

In this case we can have Joi schema validation like below.

location: Joi.object()
      .keys({
        type: Joi.string()
          .required()
          .valid(["Point"]),
        coordinates: Joi.array().ordered([
          Joi.number()
            .min(-180)
            .max(180)
            .required(),
          Joi.number()
            .min(-90)
            .max(90)
            .required()
        ])
      })
      .description("Please use this format [ longitude, latitude]")

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashrafkm picture ashrafkm  路  3Comments

a-c-m picture a-c-m  路  3Comments

alekbarszczewski picture alekbarszczewski  路  3Comments

mohamadresaaa picture mohamadresaaa  路  3Comments

neroaugustus1 picture neroaugustus1  路  4Comments