apimastery / APISimulator

API Simulator - configuration-driven tool for modeling and running of API simulations
https://apisimulator.io
3 stars 1 forks source link

Provide a way to match request body against JSON schema #2

Open x80486 opened 4 years ago

x80486 commented 4 years ago

I've read the Body Matching section. I'm aware something similar can be achieved by using JSONPath, but the outcome won't be the same. Moreover, teams already employ JSON schemas or Open API definitions to check end-point contracts — so that's a time saver.

I think it would be great is API Simulator could validate a given request body against a corresponding JSON schema. For instance, MockServer does something similar:

...
  "body": {
    "type": "JSON_SCHEMA",
    "jsonSchema": "schemas/create-entity.schema.json"
  },
...
apisim commented 4 years ago

Hi @x80486

This isn't currently supported. Labeled it as an "enhacement". Thanks!

apisim commented 4 years ago

Just to make sure I understand

validate a given request body against a corresponding JSON schema

in the context of simulating an API with API Simulator: API Simulator validating the body of an HTTP request against a given JSON Schema is as part of matching incoming requests to determine which response to return.

Is this what the feature is about?

x80486 commented 4 years ago

Yes, basically, when we model a POST operation, we usually want to make sure certain fields are there, if not, we respond accordingly. A JSON schema validates that be body sent is valid according to the schema applied to the simulation.

If you take a look at what MockServer does I think it would be easier — but it's more to match than actually validate.

apisim commented 4 years ago

Got it, thanks.

In the meantime, if you haven't already, you can check out the support for Multiple Responses in API Simulator and especially the example that demonstrates an approach to returning different responses based on the validity of input elements.

x80486 commented 5 months ago

Another approach on solving this problem could be by supporting RAML data type definitions. It makes it interesting because one can make sure the input data is actually structured in the expected form.

Something like this:

#%RAML 1.0 DataType

type: object
properties:
  employee_id: 
    type: integer
    required: true
    minimum: 8
  first_name: 
    type: string
    required: true
    minLength: 1
    maxLength: 10
    pattern: ^[A-Za-z]*
  last_name: 
    type: string
    required: true
    minLength: 1
    maxLength: 10
    pattern: ^[A-Za-z]*
  email:
    pattern: ^.+@.+\..+$
  ...

Perhaps a new DSL key to allow a reference to a file with the RAML data type definitions for a given operation. Maybe the DSL is only available for those HTTP operations that can send payloads: POST, PUT, etc.

apisim commented 5 months ago

Thank you for the suggestion, @x80486!

RAML... That takes me years back. If I recall, there were some good features at the time compared to Swagger but RAML never gained traction outside of the ecosystem of the company behind it. I may be wrong, though...

Frankly, I think supporting JSON schema validation will have larger applicability than RAML.