Validate ObjectId on test (#16579)
* feat(challenges): Validate ObjectId on test * fix(challenges): Fix 'id' field typo
This commit is contained in:
committed by
Quincy Larson
parent
0a225dce8f
commit
632c6c8840
File diff suppressed because it is too large
Load Diff
@ -2,8 +2,30 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { Observable } from 'rx';
|
import { Observable } from 'rx';
|
||||||
import tape from 'tape';
|
import tape from 'tape';
|
||||||
|
import { isMongoId } from 'validator';
|
||||||
|
|
||||||
import getChallenges from './getChallenges';
|
import getChallenges from './getChallenges';
|
||||||
|
|
||||||
|
const notMongoId = id => !isMongoId(id);
|
||||||
|
|
||||||
|
let existingIds = [];
|
||||||
|
|
||||||
|
function validateObjectId(id, title) {
|
||||||
|
if (notMongoId(id)) {
|
||||||
|
throw new Error(`Expected a vaild ObjectId for ${title}, got ${id}`);
|
||||||
|
}
|
||||||
|
const idIndex = _.findIndex(existingIds, existing => id === existing);
|
||||||
|
if (idIndex !== -1) {
|
||||||
|
throw new Error(`
|
||||||
|
All challenges must have a unique id.
|
||||||
|
|
||||||
|
The id for ${title} is already assigned
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
existingIds = [ ...existingIds, id ];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
function createIsAssert(t, isThing) {
|
function createIsAssert(t, isThing) {
|
||||||
const { assert } = t;
|
const { assert } = t;
|
||||||
return function() {
|
return function() {
|
||||||
@ -52,6 +74,7 @@ function fillAssert(t) {
|
|||||||
|
|
||||||
function createTest({
|
function createTest({
|
||||||
title,
|
title,
|
||||||
|
id = '',
|
||||||
tests = [],
|
tests = [],
|
||||||
solutions = [],
|
solutions = [],
|
||||||
head = [],
|
head = [],
|
||||||
@ -60,6 +83,7 @@ function createTest({
|
|||||||
redux = false,
|
redux = false,
|
||||||
reactRedux = false
|
reactRedux = false
|
||||||
}) {
|
}) {
|
||||||
|
validateObjectId(id, title);
|
||||||
solutions = solutions.filter(solution => !!solution);
|
solutions = solutions.filter(solution => !!solution);
|
||||||
tests = tests.filter(test => !!test);
|
tests = tests.filter(test => !!test);
|
||||||
|
|
||||||
@ -215,7 +239,7 @@ Observable.from(getChallenges())
|
|||||||
.toArray()
|
.toArray()
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(noSolutions) => {
|
(noSolutions) => {
|
||||||
if(noSolutions){
|
if (noSolutions) {
|
||||||
console.log(
|
console.log(
|
||||||
'# These challenges have no solutions\n- [ ] ' +
|
'# These challenges have no solutions\n- [ ] ' +
|
||||||
noSolutions.join('\n- [ ] ')
|
noSolutions.join('\n- [ ] ')
|
||||||
|
Reference in New Issue
Block a user