test(curriculum): challenge schema validator for lang
This commit is contained in:
@ -1,84 +1,89 @@
|
|||||||
const Joi = require('joi');
|
const Joi = require('joi');
|
||||||
Joi.objectId = require('joi-objectid')(Joi);
|
Joi.objectId = require('joi-objectid')(Joi);
|
||||||
const path = require('path');
|
|
||||||
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') });
|
|
||||||
|
|
||||||
const { LOCALE: lang = 'english' } = process.env;
|
function getSchemaForLang(lang) {
|
||||||
|
let schema = Joi.object().keys({
|
||||||
let schema = Joi.object().keys({
|
block: Joi.string(),
|
||||||
block: Joi.string(),
|
blockId: Joi.objectId(),
|
||||||
blockId: Joi.objectId(),
|
challengeOrder: Joi.number(),
|
||||||
challengeOrder: Joi.number(),
|
challengeType: Joi.number()
|
||||||
challengeType: Joi.number()
|
.min(0)
|
||||||
.min(0)
|
.max(9)
|
||||||
.max(9)
|
.required(),
|
||||||
.required(),
|
checksum: Joi.number(),
|
||||||
checksum: Joi.number(),
|
dashedName: Joi.string(),
|
||||||
dashedName: Joi.string(),
|
description: Joi.string().required(),
|
||||||
description: Joi.string().required(),
|
fileName: Joi.string(),
|
||||||
fileName: Joi.string(),
|
files: Joi.array().items(
|
||||||
files: Joi.array().items(
|
Joi.object().keys({
|
||||||
Joi.object().keys({
|
key: Joi.string(),
|
||||||
key: Joi.string(),
|
ext: Joi.string(),
|
||||||
ext: Joi.string(),
|
name: Joi.string(),
|
||||||
name: Joi.string(),
|
head: [
|
||||||
head: [Joi.array().items(Joi.string().allow('')), Joi.string().allow('')],
|
Joi.array().items(Joi.string().allow('')),
|
||||||
tail: [Joi.array().items(Joi.string().allow('')), Joi.string().allow('')],
|
Joi.string().allow('')
|
||||||
contents: [
|
],
|
||||||
Joi.array().items(Joi.string().allow('')),
|
tail: [
|
||||||
Joi.string().allow('')
|
Joi.array().items(Joi.string().allow('')),
|
||||||
]
|
Joi.string().allow('')
|
||||||
})
|
],
|
||||||
),
|
contents: [
|
||||||
guideUrl: Joi.string().uri({ scheme: 'https' }),
|
Joi.array().items(Joi.string().allow('')),
|
||||||
videoUrl: Joi.string().allow(''),
|
Joi.string().allow('')
|
||||||
helpRoom: Joi.string(),
|
]
|
||||||
id: Joi.objectId().required(),
|
})
|
||||||
instructions: Joi.string().required(),
|
),
|
||||||
isBeta: Joi.bool(),
|
guideUrl: Joi.string().uri({ scheme: 'https' }),
|
||||||
isComingSoon: Joi.bool(),
|
videoUrl: Joi.string().allow(''),
|
||||||
isLocked: Joi.bool(),
|
helpRoom: Joi.string(),
|
||||||
isPrivate: Joi.bool(),
|
id: Joi.objectId().required(),
|
||||||
isRequired: Joi.bool(),
|
instructions: Joi.string().required(),
|
||||||
name: Joi.string(),
|
isBeta: Joi.bool(),
|
||||||
order: Joi.number(),
|
isComingSoon: Joi.bool(),
|
||||||
required: Joi.array().items(
|
isLocked: Joi.bool(),
|
||||||
Joi.object().keys({
|
isPrivate: Joi.bool(),
|
||||||
link: Joi.string(),
|
isRequired: Joi.bool(),
|
||||||
raw: Joi.bool(),
|
name: Joi.string(),
|
||||||
src: Joi.string(),
|
order: Joi.number(),
|
||||||
crossDomain: Joi.bool()
|
required: Joi.array().items(
|
||||||
})
|
Joi.object().keys({
|
||||||
),
|
link: Joi.string(),
|
||||||
solutions: Joi.array().items(Joi.string().optional()),
|
raw: Joi.bool(),
|
||||||
superBlock: Joi.string(),
|
src: Joi.string(),
|
||||||
superOrder: Joi.number(),
|
crossDomain: Joi.bool()
|
||||||
suborder: Joi.number(),
|
})
|
||||||
tests: Joi.array().items(
|
),
|
||||||
// public challenges
|
solutions: Joi.array().items(Joi.string().optional()),
|
||||||
Joi.object().keys({
|
superBlock: Joi.string(),
|
||||||
text: Joi.string().required(),
|
superOrder: Joi.number(),
|
||||||
testString: Joi.string()
|
suborder: Joi.number(),
|
||||||
.allow('')
|
tests: Joi.array().items(
|
||||||
.required()
|
// public challenges
|
||||||
}),
|
Joi.object().keys({
|
||||||
// our tests used in certification verification
|
text: Joi.string().required(),
|
||||||
Joi.object().keys({
|
testString: Joi.string()
|
||||||
id: Joi.string().required(),
|
.allow('')
|
||||||
title: Joi.string().required()
|
.required()
|
||||||
})
|
}),
|
||||||
),
|
// our tests used in certification verification
|
||||||
template: Joi.string().allow(''),
|
Joi.object().keys({
|
||||||
time: Joi.string().allow(''),
|
id: Joi.string().required(),
|
||||||
title: Joi.string().required()
|
title: Joi.string().required()
|
||||||
});
|
})
|
||||||
|
),
|
||||||
if (lang !== 'english') {
|
template: Joi.string().allow(''),
|
||||||
schema = schema.append({
|
time: Joi.string().allow(''),
|
||||||
localeTitle: Joi.string().required()
|
title: Joi.string().required()
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
exports.validateChallenge = function validateChallenge(challenge) {
|
if (lang !== 'english') {
|
||||||
return Joi.validate(challenge, schema);
|
schema = schema.append({
|
||||||
|
localeTitle: Joi.string().required()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
exports.challengeSchemaValidator = lang => {
|
||||||
|
const schema = getSchemaForLang(lang);
|
||||||
|
return challenge => Joi.validate(challenge, schema);
|
||||||
};
|
};
|
||||||
|
@ -22,7 +22,7 @@ const { getChallengesForLang } = require('../getChallenges');
|
|||||||
|
|
||||||
const MongoIds = require('./utils/mongoIds');
|
const MongoIds = require('./utils/mongoIds');
|
||||||
const ChallengeTitles = require('./utils/challengeTitles');
|
const ChallengeTitles = require('./utils/challengeTitles');
|
||||||
const { validateChallenge } = require('../schema/challengeSchema');
|
const { challengeSchemaValidator } = require('../schema/challengeSchema');
|
||||||
const { challengeTypes } = require('../../client/utils/challengeTypes');
|
const { challengeTypes } = require('../../client/utils/challengeTypes');
|
||||||
|
|
||||||
const { LOCALE: lang = 'english' } = process.env;
|
const { LOCALE: lang = 'english' } = process.env;
|
||||||
@ -40,8 +40,9 @@ Mocha.Runner.prototype.fail = function(test, err) {
|
|||||||
return oldRunnerFail.call(this, test, err);
|
return oldRunnerFail.call(this, test, err);
|
||||||
};
|
};
|
||||||
|
|
||||||
let mongoIds = new MongoIds();
|
const mongoIds = new MongoIds();
|
||||||
let challengeTitles = new ChallengeTitles();
|
const challengeTitles = new ChallengeTitles();
|
||||||
|
const validateChallenge = challengeSchemaValidator(lang);
|
||||||
|
|
||||||
const { JSDOM } = jsdom;
|
const { JSDOM } = jsdom;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user