Skip to content

Commit e94b3c2

Browse files
committed
add validation function
1 parent 591626c commit e94b3c2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

dotcom-rendering/src/model/validate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import footballMatchListPageSchema from '../frontend/schemas/feFootballMatchList
1515
import footballMatchPageSchema from '../frontend/schemas/feFootballMatchPage.json';
1616
import footballTablesPageSchema from '../frontend/schemas/feFootballTablesPage.json';
1717
import frontSchema from '../frontend/schemas/feFront.json';
18+
import hostedContentSchema from '../frontend/schemas/feHostedContent.json';
1819
import tagPageSchema from '../frontend/schemas/feTagPage.json';
1920
import type { Block } from '../types/blocks';
2021
import type { FEEditionsCrosswords } from '../types/editionsCrossword';
2122
import type { FENewslettersPageType } from '../types/newslettersPage';
2223
import blockSchema from './block-schema.json';
2324
import editionsCrosswordSchema from './editions-crossword-schema.json';
2425
import newslettersPageSchema from './newsletter-page-schema.json';
26+
import { FEHostedContent } from 'src/frontend/feHostedContent';
2527

2628
const options: Options = {
2729
verbose: false,
@@ -56,6 +58,7 @@ const validateCricketMatchPage = ajv.compile<FECricketMatchPage>(
5658
const validateFootballMatchPage = ajv.compile<FEFootballMatchPage>(
5759
footballMatchPageSchema,
5860
);
61+
const validateHostedContent = ajv.compile<FEHostedContent>(hostedContentSchema);
5962

6063
export const validateAsFEArticle = (data: unknown): FEArticle => {
6164
if (validateArticle(data)) return data;
@@ -186,3 +189,15 @@ export const validateAsFootballMatchPageType = (
186189
${JSON.stringify(validateFootballMatchPage.errors, null, 2)}`,
187190
);
188191
};
192+
193+
export const validateAsHostedContent = (data: unknown): FEHostedContent => {
194+
if (validateHostedContent(data)) return data;
195+
196+
const url =
197+
isObject(data) && isString(data.webURL) ? data.webURL : 'unknown url';
198+
199+
throw new TypeError(
200+
`Unable to validate request body for url ${url}.\n
201+
${JSON.stringify(validateArticle.errors, null, 2)}`,
202+
);
203+
};

0 commit comments

Comments
 (0)