Skip to content

Commit 7e6a9ae

Browse files
committed
Relaxed validation rules + removed unknown properties
refs https://github.com/TryGhost/Toolbox/issues/314 - The API principle guiding this change is the Robustness Principle: "be conservative in what you send, be liberal in what you accept". The API will start accepting any additional properties that are not explicitly defined in the schema for the resource and will be trimming any rogue properties that are sent in the payload
1 parent 460d932 commit 7e6a9ae

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/admin-api-schema/lib/utils/json-schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const errors = require('@tryghost/errors');
77
const ajv = new Ajv({
88
allErrors: true,
99
useDefaults: true,
10+
removeAdditional: true,
1011
formats: {
1112
'json-string': (data) => {
1213
try {

packages/admin-api-schema/test/api.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ describe('Exposes a correct API', function () {
6666
}
6767
});
6868

69+
it('Unknown fields get ignored and trimmed', async function () {
70+
const data = {
71+
posts: [{
72+
title: 'valid',
73+
author: 'Beccy',
74+
something: 'else'
75+
}]
76+
};
77+
78+
try {
79+
should.equal(data.posts[0].something, 'else');
80+
should.equal(data.posts[0].author, 'Beccy');
81+
await apiSchema.validate({data, schema: 'posts-add', definition: 'posts'});
82+
should.equal(data.posts[0].something, undefined);
83+
should.equal(data.posts[0].author, undefined);
84+
} catch (err) {
85+
throw new Error('should not throw an error');
86+
}
87+
});
88+
6989
it('Incorrect use throws an error', async function () {
7090
const data = {
7191
posts: [{

0 commit comments

Comments
 (0)