Added custom json-string format to AJV validator

no issue

- the value of `mobiledoc` when submitting a page/post via the API must
  be JSON, but we don't validate this
- this results in url-utils throwing an error, which ends up being a 500
- this commit adds a custom format to AJV to validate it is valid JSON
- also updates tests with bad JSON - 'a'
This commit is contained in:
Daniel Lockyer 2020-04-16 18:43:40 +01:00
parent 374b43ceab
commit 8853067830
12 changed files with 32 additions and 8 deletions

View File

@ -19,6 +19,7 @@
},
"mobiledoc": {
"type": ["string", "null"],
"format": "json-string",
"maxLength": 1000000000
},
"html": {

View File

@ -19,6 +19,7 @@
},
"mobiledoc": {
"type": ["string", "null"],
"format": "json-string",
"maxLength": 1000000000
},
"html": {

View File

@ -5,7 +5,17 @@ const common = require('../../../../../lib/common');
const ajv = new Ajv({
allErrors: true,
useDefaults: true
useDefaults: true,
formats: {
'json-string': (data) => {
try {
JSON.parse(data);
return true;
} catch (e) {
return false;
}
}
}
});
stripKeyword(ajv);

View File

@ -19,6 +19,7 @@
},
"mobiledoc": {
"type": ["string", "null"],
"format": "json-string",
"maxLength": 1000000000
},
"html": {

View File

@ -19,6 +19,7 @@
},
"mobiledoc": {
"type": ["string", "null"],
"format": "json-string",
"maxLength": 1000000000
},
"html": {

View File

@ -5,7 +5,17 @@ const common = require('../../../../../lib/common');
const ajv = new Ajv({
allErrors: true,
useDefaults: true
useDefaults: true,
formats: {
'json-string': (data) => {
try {
JSON.parse(data);
return true;
} catch (e) {
return false;
}
}
}
});
stripKeyword(ajv);

View File

@ -140,7 +140,7 @@ describe('Unit: canary/utils/validators/input/pages', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],

View File

@ -140,7 +140,7 @@ describe('Unit: canary/utils/validators/input/posts', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],

View File

@ -140,7 +140,7 @@ describe('Unit: v2/utils/validators/input/pages', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],

View File

@ -140,7 +140,7 @@ describe('Unit: v2/utils/validators/input/posts', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],

View File

@ -140,7 +140,7 @@ describe('Unit: v3/utils/validators/input/pages', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],

View File

@ -140,7 +140,7 @@ describe('Unit: v3/utils/validators/input/posts', function () {
const fieldMap = {
title: [123, new Date(), _.repeat('a', 2001)],
slug: [123, new Date(), _.repeat('a', 192)],
mobiledoc: [123, new Date()],
mobiledoc: [123, new Date(), 'a'],
feature_image: [123, new Date(), 'random words'],
featured: [123, new Date(), 'abc'],
status: [123, new Date(), 'abc'],