This site uses cookies own and third. If you continue to browse consider to accept the use of cookies. OK More Info.

[SOLUCIONADO] Problema al validar schema en Postman

  • 1 Replies
  • 936 Views

0 Members and 2 Guests are viewing this topic.

Online ANTRAX

  • *
  • Administrator
  • Posts: 5839
  • Actividad:
    36.67%
  • Country: ar
  • Reputación 43
  • ANTRAX
  • Twitter: @Underc0de
    • View Profile
    • Underc0de
    • Email

[SOLUCIONADO] Problema al validar schema en Postman

  • on: March 04, 2022, 11:10:52 am
Hola a todos! Estoy intentando validar un schema con postman. Utilicé la siguiente web para convertir el JSON en Schema: You are not allowed to view links. Register or Login
Y el resultado fue este:

Code: (javascript) You are not allowed to view links. Register or Login
const schema = {
  "type": "object",
  "properties": {
    "system_id": {"type": "string"},
    "start": {"type": "string"},
    "end": {"type": "string"},
    "period": {"type": "string" },
    "unit": {"type": "string"},
    "values": {"type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "timestamp": {"type": "string"},
            "battery_charge": {"type": "float" },
            "battery_discharge": {"type": "number"},
            "grid_export": {"type": "number"},
            "grid_import": {"type": "number"},
            "home_consumption": {"type": "number"},
            "solar": {"type": "number"},
            "battery_charge_state": {"type": "number"}
          },
          "required": [
            "timestamp",
            "battery_charge",
            "battery_discharge",
            "grid_export",
            "grid_import",
            "home_consumption",
            "solar",
            "battery_charge_state"
          ]
        }
      ]
    },
    "totals": {
      "type": "object",
      "properties": {
        "battery_charge": {"type": "number"},
        "battery_discharge": {"type": "number"},
        "grid_export": {"type": "number"},
        "grid_import": {"type": "number"},
        "home_consumption": {"type": "number"},
        "solar": {"type": "number"}
      },
      "required": [
        "battery_charge",
        "battery_discharge",
        "grid_export",
        "grid_import",
        "home_consumption",
        "solar"
      ]
    }
  },
  "required": [
    "system_id",
    "start",
    "end",
    "period",
    "unit",
    "values",
    "totals"
  ]
}

pm.test("Schema validation", () => {
    pm.response.to.have.jsonSchema(schema);
});

El problema esta en que coloca varios "Numbers" en alguno de los elementos. Por ejemplo:

"battery_discharge": {"type": "number"},

Y en realidad debería ser un "float". Pero cuando cambio esto, inmediatamente postman me tira el siguiente error:

Code: (text) You are not allowed to view links. Register or Login
Schema validation | Error: schema is invalid: data.properties['values'].items should be object,boolean, data.properties['values'].items[0].properties['battery_charge'].type should be equal to one of the allowed values, data.properties['values'].items[0].properties['battery_charge'].type should be array, data.properties['values'].items[0].properties['battery_charge'].type should match some schema in anyOf, data.properties['values'].items should match some schema in anyOf
Alguna sugerencia?

Saludos,
ANTRAX
« Last Edit: March 07, 2022, 02:12:30 pm by ANTRAX »


Marked as best answer by ANTRAX on March 07, 2022, 02:12:08 pm

Online ANTRAX

  • *
  • Administrator
  • Posts: 5839
  • Actividad:
    36.67%
  • Country: ar
  • Reputación 43
  • ANTRAX
  • Twitter: @Underc0de
    • View Profile
    • Underc0de
    • Email

Re: Problema al validar schema en Postman

  • on: March 07, 2022, 02:11:57 pm
Después de investigar un buen rato, encontré la respuesta.

Cuando se validan Jsons con postman, los 6 tipos de datos permitidos son: string, number, array, object, null, boolean. Por lo tanto, si el dato es de tipo float, corresponde a "number"

Espero que les sirva.

Saludos,
ANTRAX