swagger-express-middleware
swagger-express-middleware copied to clipboard
How i can add new format validation?
In spec 2.0 for strings have 5 build-in formats: password, date, date-time, binary and byte But it is also written that: "However, the format property is an open string-valued property, and can have any value to support documentation needs. Formats such as "email", "uuid", etc., can be used even though they are not defined by this specification."
I want add email validation. Library tv4 have method ValidatorContext.prototype.addFormat, which can be used for that, but "swagger-express-middleware" can't yet use user-defined formats...
It turned out that the formats can be added directly using tv4. No need proxy formats inside your library. I think you can write about it in documentation - user-defined formats is very useful feature Example:
var tv4 = require('tv4');
var formats = {
email: function(data, schema) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (re.test(data))
return null;
else
return "Wrong Email format";
}
};
tv4.addFormat(formats);
Looks like tv4 library very slow, you are considering - the transition to a faster library? For example: https://github.com/epoberezkin/ajv or https://github.com/mafintosh/is-my-json-valid
Hi, @Santinell. Sorry for taking so long to reply. I just started a new job, so my schedule has been super busy lately. o_O
I'm currently in the process of switching from tv4 to Z-Schema, which has many more features, better performance, and is more compliant with the specs. I've already made this switch in Swagger Parser, which is used internally by Swagger Express Middleware. So, right now, Swagger Express Middlware actually contains tv4 and Z-Schema, but that won't be the case for much longer
And, to answer your other question... yes, I will provide a way for you register custom format validators. Z-Schema makes this very easy
How switching process? maybe you need help? I can try make PR for z-schema using
By the way... ajv (fastest json-schema validator) now supports some features of json-schema v5 and data coercion ;) (not advertising)
Z-schema throw error on test "JSON Schema - parse date-time params should parse a valid date-time param" "Keyword 'maximum' is expected to be of type 'number'"
var schema = {
type: 'string',
format: 'date-time',
minimum: new Date(Date.UTC(2010, 0, 1)),
exclusiveMinimum: true,
maximum: '2010-12-31T23:59:59.999Z',
exclusiveMaximum: false
};
Looks like using for minumum and maximum not stings is wrong: http://swagger.io/specification/
If remove tests with maximum/minimum of date and date-time then tests stops on: "JSON Schema - parse file params should parse a valid file param" With error "Keyword 'required' is expected to be of type 'array'"
var value = 'fido',
schema = { name: 'PetName',
in: 'path',
description: 'name of the pet',
required: true,
type: 'string' };
Looks like you need somehow modify parameter schema before validation or join all params in some object and validate it once. For example:
parameters: [
{
in:'body',
name: 'data',
required: true,
schema: {...}
},
{
in: 'path',
name: 'name',
required: true,
type: 'string'
}
]
----->
params: {
body: {
type: 'object',
properties: {
data: {
schema: {...}
}
},
required: ['data']
},
path: {
type: 'object',
properties: {
name: {
type: 'string'
}
},
required: ['name']
}
}