feat(schema): Implement challenge schema

pull/18182/head
Stuart Taylor 2018-03-23 14:35:29 +00:00 committed by Mrugesh Mohapatra
parent b7930795d6
commit c754880476
2 changed files with 122 additions and 4 deletions

View File

@ -9,6 +9,7 @@ const utils = require('../server/utils');
const getChallenges = require('./getChallenges'); const getChallenges = require('./getChallenges');
const app = require('../server/server'); const app = require('../server/server');
const createDebugger = require('debug'); const createDebugger = require('debug');
const { validateChallenge } = require('./schema/challengeSchema');
const log = createDebugger('fcc:seed'); const log = createDebugger('fcc:seed');
// force logger to always output // force logger to always output
@ -108,7 +109,7 @@ Observable.combineLatest(
challenge.order = order; challenge.order = order;
challenge.suborder = index + 1; challenge.suborder = index + 1;
challenge.block = dasherize(blockName); challenge.block = dasherize(blockName);
challenge.blockId = block.id; challenge.blockId = '' + block.id;
challenge.isBeta = challenge.isBeta || isBeta; challenge.isBeta = challenge.isBeta || isBeta;
challenge.isComingSoon = challenge.isComingSoon || isComingSoon; challenge.isComingSoon = challenge.isComingSoon || isComingSoon;
challenge.isLocked = challenge.isLocked || isLocked; challenge.isLocked = challenge.isLocked || isLocked;
@ -123,11 +124,35 @@ Observable.combineLatest(
.join(' '); .join(' ');
challenge.required = (challenge.required || []).concat(required); challenge.required = (challenge.required || []).concat(required);
challenge.template = challenge.template || template; challenge.template = challenge.template || template;
return _.omit(
return challenge; challenge,
[
'betaSolutions',
'betaTests',
'hints',
'MDNlinks',
'null',
'rawSolutions',
'react',
'reactRedux',
'redux',
'releasedOn',
'translations',
'type'
]
);
}); });
}) })
.flatMap(challenges => createChallenges(challenges)); .flatMap(challenges => {
challenges.forEach(challenge => {
const result = validateChallenge(challenge);
if (result.error) {
console.log(result.value);
throw new Error(result.error);
}
});
return createChallenges(challenges);
});
}) })
.subscribe( .subscribe(
function(challenges) { function(challenges) {

93
schema/challengeSchema.js Normal file
View File

@ -0,0 +1,93 @@
const Joi = require('joi');
Joi.objectId = require('joi-objectid')(Joi);
const schema = Joi.object().keys({
block: Joi.string(),
blockId: Joi.objectId(),
challengeSeed: Joi.array().items(
Joi.string().allow('')
),
challengeType: Joi.number().min(0).max(9).required(),
checksum: Joi.number(),
dashedName: Joi.string(),
description: Joi.array().items(
// classic/modern challenges
Joi.string().allow(''),
// step challenges
Joi.array().items(
Joi.string().allow('')
).length(4),
// quiz challenges
Joi.object().keys({
subtitle: Joi.string(),
question: Joi.string(),
choices: Joi.array(),
answer: Joi.number(),
explanation: Joi.string()
})
).required(),
fileName: Joi.string(),
files: Joi.object().pattern(
/(jsx?|html|css|sass)$/,
Joi.object().keys({
key: Joi.string(),
ext: Joi.string(),
name: Joi.string(),
head: Joi.string().allow(''),
tail: Joi.string().allow(''),
contents: Joi.string()
})
),
guideUrl: Joi.string().uri({ scheme: 'https' }),
head: Joi.array().items(
Joi.string().allow('')
),
helpRoom: Joi.string(),
id: Joi.objectId().required(),
isBeta: Joi.bool(),
isComingSoon: Joi.bool(),
isLocked: Joi.bool(),
isPrivate: Joi.bool(),
isRequired: Joi.bool(),
name: Joi.string(),
order: Joi.number(),
required: Joi.array().items(
Joi.object().keys({
link: Joi.string(),
raw: Joi.bool(),
src: Joi.string(),
crossDomain: Joi.bool()
})
),
solutions: Joi.array().items(
Joi.string().optional()
),
superBlock: Joi.string(),
superOrder: Joi.number(),
suborder: Joi.number(),
tail: Joi.array().items(
Joi.string().allow('')
),
tests: Joi.array().items(
Joi.string().min(2),
Joi.object().keys({
text: Joi.string().required(),
testString: Joi.string().allow('').required()
}),
Joi.object().keys({
id: Joi.objectId().required(),
title: Joi.string().required()
})
),
template: Joi.string(),
time: Joi.string().allow(''),
title: Joi.string().required()
});
exports.validateChallenge = function validateChallenge(challenge) {
return Joi.validate(challenge, schema);
};