Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6a66cd

Browse files
committed
coderoadJson validator
1 parent 69c0755 commit a6a66cd

File tree

11 files changed

+177
-17
lines changed

11 files changed

+177
-17
lines changed

lib/cli.js

100755100644
File mode changed.
File renamed without changes.

lib/tutorials/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22
var chalk_1 = require('chalk');
33
var find_tutorials_1 = require('./find-tutorials');
4-
var get_1 = require('../packageJson/get');
4+
var packageJson_1 = require('../get/packageJson');
55
function tutorials(dir) {
6-
var pj = get_1.default(dir);
6+
var pj = packageJson_1.default(dir);
77
if (!pj) {
88
console.log(chalk_1.red("No package.json available"));
99
return null;

lib/validate/coderoadJson.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"use strict";
2+
var validKeys = {
3+
info: ['title', 'description'],
4+
page: ['title', 'description', 'onPageComplete', 'tasks', 'video', 'link'],
5+
task: ['description', 'tests', 'actions', 'hints'],
6+
actions: ['open', 'set', 'insert'],
7+
};
8+
function validateCoderoadJson(json) {
9+
var errors = [];
10+
var warnings = [];
11+
try {
12+
json.parse(json);
13+
}
14+
catch (e) {
15+
errors.push({
16+
name: 'json',
17+
msg: 'is invalid'
18+
});
19+
return {
20+
errors: errors, warnings: warnings
21+
};
22+
}
23+
var infoKeys = Object.keys(json.info);
24+
infoKeys.forEach(function (key) {
25+
if (validKeys.info.indexOf(key) < 0) {
26+
errors.push({
27+
name: key,
28+
msg: 'is missing',
29+
location: "info." + key,
30+
});
31+
}
32+
});
33+
json.pages.forEach(function (page, pIndex) {
34+
var pageKeys = Object.keys(page);
35+
pageKeys.forEach(function (key) {
36+
if (validKeys.page.indexOf(key) < 0) {
37+
errors.push({
38+
name: key,
39+
msg: 'is an invalid key',
40+
location: "pages[" + pIndex + "]",
41+
example: json.pages[pIndex],
42+
});
43+
}
44+
});
45+
if (page.tasks && page.tasks.length > 0) {
46+
page.tasks.forEach(function (task, tIndex) {
47+
var taskKeys = Object.keys(task);
48+
taskKeys.forEach(function (key) {
49+
if (validKeys.task.indexOf(key) < 0) {
50+
errors.push({
51+
name: 'page task',
52+
msg: "Invalid Task key \"" + key + "\"",
53+
location: "pages[" + pIndex + "].tasks[]" + tIndex + "]",
54+
});
55+
}
56+
});
57+
if (!task.tests || task.tests.length < 1) {
58+
errors.push({
59+
name: 'task test',
60+
msg: 'Missing Task Test',
61+
location: "pages[" + pIndex + "].tasks[" + tIndex + "]",
62+
});
63+
}
64+
});
65+
}
66+
else {
67+
warnings.push({
68+
name: 'page tasks',
69+
msg: 'are missing',
70+
location: "pages[" + pIndex + "]",
71+
});
72+
}
73+
});
74+
return {
75+
errors: errors,
76+
warnings: warnings,
77+
};
78+
}
79+
Object.defineProperty(exports, "__esModule", { value: true });
80+
exports.default = validateCoderoadJson;

lib/validate/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22
var chalk_1 = require('chalk');
33
var packageJson_1 = require('./packageJson');
4-
var get_1 = require('../packageJson/get');
4+
var packageJson_2 = require('../get/packageJson');
55
function validate() {
6-
var pj = get_1.default(process.cwd());
6+
var pj = packageJson_2.default(process.cwd());
77
if (!pj) {
88
console.log(chalk_1.red('Error: No package.json.'));
99
return false;

lib/validate/name.js

Lines changed: 0 additions & 10 deletions
This file was deleted.
File renamed without changes.

src/tutorials/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {red} from 'chalk';
22
import findTutorials from './find-tutorials';
3-
import getPackageJson from '../packageJson/get';
3+
import getPackageJson from '../get/packageJson';
44

55
export default function tutorials(dir: string): string[] {
66
const pj: PackageJson = getPackageJson(dir);

src/validate/coderoadJson.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const validKeys = {
2+
info: ['title', 'description'],
3+
page: ['title', 'description', 'onPageComplete', 'tasks', 'video', 'link'],
4+
task: ['description', 'tests', 'actions', 'hints'],
5+
actions: ['open', 'set', 'insert'],
6+
};
7+
8+
9+
export default function validateCoderoadJson(json): ValidatePjOutput {
10+
const errors = [];
11+
const warnings = [];
12+
13+
// test for invalid json
14+
try {
15+
json.parse(json);
16+
} catch (e) {
17+
errors.push({
18+
name: 'json',
19+
msg: 'is invalid'
20+
});
21+
return {
22+
errors, warnings
23+
};
24+
}
25+
26+
// info =>
27+
const infoKeys = Object.keys(json.info);
28+
infoKeys.forEach((key) => {
29+
if (validKeys.info.indexOf(key) < 0) {
30+
errors.push({
31+
name: key,
32+
msg: 'is missing',
33+
location: `info.${key}`,
34+
});
35+
}
36+
});
37+
38+
json.pages.forEach((page: CR.Page, pIndex: number) => {
39+
40+
// pages =>
41+
const pageKeys = Object.keys(page);
42+
pageKeys.forEach((key) => {
43+
if (validKeys.page.indexOf(key) < 0) {
44+
errors.push({
45+
name: key,
46+
msg: 'is an invalid key',
47+
location: `pages[${pIndex}]`,
48+
example: json.pages[pIndex],
49+
});
50+
}
51+
});
52+
53+
// pages => tasks
54+
if (page.tasks && page.tasks.length > 0) {
55+
page.tasks.forEach((task: CR.Task, tIndex: number) => {
56+
let taskKeys = Object.keys(task);
57+
58+
taskKeys.forEach((key) => {
59+
if (validKeys.task.indexOf(key) < 0) {
60+
errors.push({
61+
name: 'page task',
62+
msg: `Invalid Task key "${key}"`,
63+
location: `pages[${pIndex}].tasks[]${tIndex}]`,
64+
});
65+
}
66+
});
67+
68+
if (!task.tests || task.tests.length < 1) {
69+
errors.push({
70+
name: 'task test',
71+
msg: 'Missing Task Test',
72+
location: `pages[${pIndex}].tasks[${tIndex}]`,
73+
});
74+
}
75+
});
76+
} else {
77+
warnings.push({
78+
name: 'page tasks',
79+
msg: 'are missing',
80+
location: `pages[${pIndex}]`,
81+
});
82+
}
83+
});
84+
85+
return {
86+
errors,
87+
warnings,
88+
};
89+
}

src/validate/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {red, yellow} from 'chalk';
22
import validatePackageJson from './packageJson';
3-
import getPackageJson from '../packageJson/get';
3+
import getPackageJson from '../get/packageJson';
44

55
export default function validate(): boolean {
66
const pj = getPackageJson(process.cwd());

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"src/create/index.ts",
3232
"src/create/validate.ts",
3333
"src/create/write-demo.ts",
34-
"src/packageJson/get.ts",
34+
"src/get/packageJson.ts",
3535
"src/publish/index.ts",
3636
"src/publish/validate.ts",
3737
"src/search/index.ts",
@@ -59,6 +59,7 @@
5959
"src/typings/sort-package-json/index.d.ts",
6060
"src/typings/tsd.d.ts",
6161
"src/update/index.ts",
62+
"src/validate/coderoadJson.ts",
6263
"src/validate/index.ts",
6364
"src/validate/packageJson.ts"
6465
],

0 commit comments

Comments
 (0)