Skip to content

Commit

Permalink
Update CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Mar 13, 2024
1 parent 8bd76f1 commit c02eb32
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 2,028 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

## Version History

### v10.7.0

- :data: Enfoce `res.json` with AJV

### v10.6.0

- :bug: Properties that are not defined in the Schema are always removed
Expand Down
60 changes: 60 additions & 0 deletions lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class Schemas {
this.docs.push({ method: Doc.HttpMethods.GET, path: path }, opts);
this.schemas.set(`GET ${path}`, opts);

const resValidation = opts.res && ajv.compile(opts.res);
const paramsValidation = opts.params && ajv.compile(opts.params);
const queryValidation = opts.query && ajv.compile(opts.query);
if (opts.body) throw new Error(`Body not allowed`);
Expand All @@ -123,6 +124,17 @@ export default class Schemas {
if (queryValidation && !queryValidation(req.query)) errors.push({ type: 'Query', errors: queryValidation.errors as ErrorObject[] });
if (errors.length) return Err.respond(new Err(400, null, 'Validation Error'), res, errors);

const json = res.json;
res.json = function(obj) {
obj = JSON.parse(JSON.stringify(obj)) // Here as Date => String needs to happen
if ((res.statusCode === null || res.statusCode === 200) && resValidation && !resValidation(obj)) {
res.status(400);
return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] });
} else {
return json.call(this, obj);
}
};

return handler(req, res, next);
};

Expand All @@ -141,6 +153,7 @@ export default class Schemas {
this.docs.push({ method: Doc.HttpMethods.DELETE, path: path }, opts);
this.schemas.set(`DELETE ${path}`, opts);

const resValidation = opts.res && ajv.compile(opts.res);
const paramsValidation = opts.params && ajv.compile(opts.params);
const queryValidation = opts.query && ajv.compile(opts.query);
if (opts.body) throw new Error(`Body not allowed`);
Expand All @@ -151,6 +164,17 @@ export default class Schemas {
if (queryValidation && !queryValidation(req.query)) errors.push({ type: 'Query', errors: queryValidation.errors as ErrorObject[] });
if (errors.length) return Err.respond(new Err(400, null, 'Validation Error'), res, errors);

const json = res.json;
res.json = function(obj) {
obj = JSON.parse(JSON.stringify(obj)) // Here as Date => String needs to happen
if ((res.statusCode === null || res.statusCode === 200) && resValidation && !resValidation(obj)) {
res.status(400);
return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] });
} else {
return json.call(this, obj);
}
};

return handler(req, res, next);
};

Expand All @@ -169,6 +193,7 @@ export default class Schemas {
this.docs.push({ method: Doc.HttpMethods.POST, path: path }, opts);
this.schemas.set(`POST ${path}`, opts);

const resValidation = opts.res && ajv.compile(opts.res);
const paramsValidation = opts.params && ajv.compile(opts.params);
const queryValidation = opts.query && ajv.compile(opts.query);
const bodyValidation = opts.body && ajv.compile(opts.body);
Expand All @@ -180,6 +205,17 @@ export default class Schemas {
if (bodyValidation && !bodyValidation(req.body)) errors.push({ type: 'Body', errors: bodyValidation.errors as ErrorObject[] });
if (errors.length) return Err.respond(new Err(400, null, 'Validation Error'), res, errors);

const json = res.json;
res.json = function(obj) {
obj = JSON.parse(JSON.stringify(obj)) // Here as Date => String needs to happen
if ((res.statusCode === null || res.statusCode === 200) && resValidation && !resValidation(obj)) {
res.status(400);
return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] });
} else {
return json.call(this, obj);
}
};

return handler(req, res, next);
};

Expand All @@ -198,6 +234,7 @@ export default class Schemas {
this.docs.push({ method: Doc.HttpMethods.PATCH, path: path }, opts);
this.schemas.set(`PATCH ${path}`, opts);

const resValidation = opts.res && ajv.compile(opts.res);
const paramsValidation = opts.params && ajv.compile(opts.params);
const queryValidation = opts.query && ajv.compile(opts.query);
const bodyValidation = opts.body && ajv.compile(opts.body);
Expand All @@ -209,6 +246,17 @@ export default class Schemas {
if (bodyValidation && !bodyValidation(req.body)) errors.push({ type: 'Body', errors: bodyValidation.errors as ErrorObject[] });
if (errors.length) return Err.respond(new Err(400, null, 'Validation Error'), res, errors);

const json = res.json;
res.json = function(obj) {
obj = JSON.parse(JSON.stringify(obj)) // Here as Date => String needs to happen
if ((res.statusCode === null || res.statusCode === 200) && resValidation && !resValidation(obj)) {
res.status(400);
return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] });
} else {
return json.call(this, obj);
}
};

return handler(req, res, next);
};

Expand All @@ -227,6 +275,7 @@ export default class Schemas {
this.docs.push({ method: Doc.HttpMethods.PUT, path: path }, opts);
this.schemas.set(`PUT ${path}`, opts);

const resValidation = opts.res && ajv.compile(opts.res);
const paramsValidation = opts.params && ajv.compile(opts.params);
const queryValidation = opts.query && ajv.compile(opts.query);
const bodyValidation = opts.body && ajv.compile(opts.body);
Expand All @@ -238,6 +287,17 @@ export default class Schemas {
if (bodyValidation && !bodyValidation(req.body)) errors.push({ type: 'Body', errors: bodyValidation.errors as ErrorObject[] });
if (errors.length) return Err.respond(new Err(400, null, 'Validation Error'), res, errors);

const json = res.json;
res.json = function(obj) {
obj = JSON.parse(JSON.stringify(obj)) // Here as Date => String needs to happen
if ((res.statusCode === null || res.statusCode === 200) && resValidation && !resValidation(obj)) {
res.status(400);
return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] });
} else {
return json.call(this, obj);
}
};

return handler(req, res, next);
};

Expand Down
Loading

0 comments on commit c02eb32

Please sign in to comment.