mikunn / openapi-schema-to-json-schema

Converts OpenAPI Schema Object to JSON Schema
75 stars 6 forks source link

[Feature] Add support for `allowEmptyValue` #8

Closed ehmicky closed 6 years ago

ehmicky commented 6 years ago

Should allowEmptyValue be converted to minLength: 1? At the moment, it is just deleted.

The specification is not really clear when it comes to the meaning of the word empty, but I take it they mean empty string. It's also not clear whether empty is after trimming or not. Since the minLength keyword would have no effect when type is not string, it might not be necessary to check for type to perform the conversion.

If minLength is already specified, the maximum should probably be taken.

Alternatives to minLength could be using not: { const } (JSON schema version 6 only) and using pattern (might be less performant, and would require potentially merging several patterns).

I've send an issue on the OpenAPI specification for possible clarification.

mikunn commented 6 years ago

This package supports only the Schema Object. I believe allowEmptyValue can only be specified in the root level of Parameter Object. Or am I mistaken?

ehmicky commented 6 years ago

Thanks for your response.

Yes indeed, that is true.

Could this be a good idea to allow passing a parameter or array of parameters as input? There are several properties that could be re-used as JSON schema properties: name, description, required, allowEmptyValue, allowReserved.

For example:

openapiSchemaToJsonSchema.fromParameter([{
  name: 'paramName',
  in: 'query',
  description: 'param description',
  required: true,
  allowEmptyValue: true,
  allowReserved: false,
  schema: {
    type: 'string',
  },
}]);

would return:

{
  type: 'object'
  properties: {
    paramName: {
      type: 'string'
      minLength: 1,
      pattern: '...' // For `allowReserved`
    }
  }
  required: ['paramName']
}

I understand though the motivation to keep this library small and only as a translation layer between OpenAPI schemas and JSON schemas.

My use case would be when this library is used to get as much information as possible from OpenAPI into JSON schemas to (for example) validate the parameters. There are several libraries that do this by hand, for example Sway.

mikunn commented 6 years ago

Thanks for the example! I can definitely see the use case. However, I would like to keep this package strictly for converting from Schema Object.

I think the best option could be to write a new package that uses this package to convert the schema portion and then augments the result based on the parameter options as necessary (adding minLength and stuff). Then it would construct the final JSON schema from each of these "sub-schemas".

This package should probably have a flag to not include the schema declaration ($schema).

ehmicky commented 6 years ago

I understand, that makes perfect sense. I might end up creating that package, depending on time constraints. If I do, I'll keep you posted :smile:

mikunn commented 6 years ago

OK, cool :smiley: I created an issue about the option to leave out $schema.