Forum Discussion

Lucaslyu's avatar
Lucaslyu
New Contributor
4 years ago

Validate JSON Payload in SwaggerHub

how can i validate JSON payload in Swaggerhub, and response 200 for validate and 500 for invalidate based on schema?

  • because there is no build in function to validate JSON Payload in Swaggerhub, so i created a JSON Payload validation method running in Azure Function that converts SwaggerHub OpenAPI spec to Json schema specification (2020-12), use newtonsoft to validate the Json Data Payload used in the SwaggerHub API.

     

    Example:

    string schema = ConvertToJsonSchema(OpenApiJson)

    JSchema jsonSchema = JSchema.Parse(schema);
    JObject jsonData = JObject.Parse(jsonPayload);

    IList<string> errors = new List<string>();
    bool valid = jsonData.IsValid(jsonSchema, out errors);

  • Lucaslyu's avatar
    Lucaslyu
    New Contributor

    because there is no build in function to validate JSON Payload in Swaggerhub, so i created a JSON Payload validation method running in Azure Function that converts SwaggerHub OpenAPI spec to Json schema specification (2020-12), use newtonsoft to validate the Json Data Payload used in the SwaggerHub API.

     

    Example:

    string schema = ConvertToJsonSchema(OpenApiJson)

    JSchema jsonSchema = JSchema.Parse(schema);
    JObject jsonData = JObject.Parse(jsonPayload);

    IList<string> errors = new List<string>();
    bool valid = jsonData.IsValid(jsonSchema, out errors);