Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

70 lines
2.1 KiB
JavaScript

const Joi = require('joi');
const blockIntroSchema = Joi.object()
.keys({
title: Joi.string().trim().min(1).required(),
intro: Joi.array().items(Joi.string().allow('')).required()
})
.unknown(true);
const superBlockIntroSchema = Joi.object()
.keys({
title: Joi.string().trim().min(1).required(),
intro: Joi.array().items(Joi.string().allow('')).required(),
blocks: Joi.object().pattern(Joi.string(), blockIntroSchema).required()
})
.unknown(true);
function createIntroSchema(expectedBlocksBySuperblock) {
return Joi.object()
.unknown(true)
.custom((intros, helpers) => {
for (const [superblock, blocks] of Object.entries(
expectedBlocksBySuperblock
)) {
const superBlockIntro = intros[superblock];
if (!superBlockIntro) {
return helpers.error('any.custom', {
message: `Missing intro.json entry for superblock "${superblock}"`
});
}
const { error: superBlockError } =
superBlockIntroSchema.validate(superBlockIntro);
if (superBlockError) {
return helpers.error('any.custom', {
message: `Invalid intro.json shape for superblock "${superblock}": ${superBlockError.message}`
});
}
for (const block of blocks) {
const blockIntro = superBlockIntro.blocks?.[block];
if (!blockIntro) {
return helpers.error('any.custom', {
message: `Missing intro.json block title entry for "${superblock}/${block}"`
});
}
const { error: blockError } = blockIntroSchema.validate(blockIntro);
if (blockError) {
return helpers.error('any.custom', {
message: `Invalid intro.json block entry for "${superblock}/${block}": ${blockError.message}`
});
}
}
}
return intros;
}, 'intro block coverage validation')
.messages({
'any.custom': '{{#message}}'
});
}
exports.validateIntroSchema = (intros, expectedBlocksBySuperblock) => {
return createIntroSchema(expectedBlocksBySuperblock).validate(intros, {
abortEarly: false
});
};