Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf29235a

Browse files
committed
validate schema with tests
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parentcc46fed commitf29235a

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

‎src/utils/schema/meta.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
exportdefault{
22
$schema:"http://json-schema.org/draft-07/schema#",
3-
$id:"http://coderoad.io/tutorial_version.schema.json",
4-
title:"TutorialVersion",
3+
$id:"https://coderoad.io/tutorial-schema.json",
4+
title:"TutorialSchema",
55
description:
6-
"A CodeRoad tutorialversion. This JSON data is converted into a tutorial with the CodeRoad editor extension",
6+
"A CodeRoad tutorialschema data. This JSON data is converted into a tutorial with the CodeRoad editor extension",
77
definitions:{
88
semantic_version:{
99
type:"string",
@@ -39,7 +39,7 @@ export default {
3939
"An array of files which will be opened by the editor when entering the level or step",
4040
items:{
4141
$ref:"#/definitions/file_path",
42-
uniqueItems:true,
42+
//uniqueItems: true,
4343
},
4444
},
4545
command_array:{
@@ -57,7 +57,7 @@ export default {
5757
"An array of git commits which will be loaded when the level/step or solution is loaded",
5858
items:{
5959
$ref:"#/definitions/sha1_hash",
60-
uniqueItems:true,
60+
//uniqueItems: true,
6161
},
6262
minItems:1,
6363
},
@@ -79,7 +79,7 @@ export default {
7979
type:"array",
8080
items:{
8181
$ref:"#/definitions/file_path",
82-
uniqueItems:true,
82+
//uniqueItems: true,
8383
},
8484
description:
8585
"An array file paths that, when updated, will trigger the test runner to run",

‎src/utils/validate.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import*asTfrom"../../typings/tutorial";
21
importschemafrom"./schema";
32

43
// https://www.npmjs.com/package/ajv
54
//@ts-ignore ajv typings not working
65
importJsonSchemafrom"ajv";
76

8-
exportfunctionvalidateSchema(json:any){
7+
exportfunctionvalidateSchema(json:any):boolean|PromiseLike<boolean>{
98
// validate using https://json-schema.org/
10-
constjsonSchema=newJsonSchema({allErrors:true,verbose:true});
11-
// support draft-07 of json schema
12-
jsonSchema.addMetaSchema(require("ajv/lib/refs/json-schema-draft-07.json"));
9+
constjsonSchema=newJsonSchema({
10+
allErrors:true,
11+
// verbose: true,
12+
});
1313

14-
constvalidator=jsonSchema.compile(schema);
15-
constvalid=validator(json);
14+
constvalid=jsonSchema.validate(schema,json);
1615

1716
if(!valid){
1817
// log errors
19-
console.log(jsonSchema.errorsText());
20-
thrownewError("Invalid schema. See log for details");
18+
if(process.env.NODE_ENV!=="test"){
19+
console.error("Validation failed. See below for details");
20+
jsonSchema.errors?.forEach((error:JsonSchema.ErrorObject)=>{
21+
console.warn(
22+
`Validation error at${error.dataPath} -${error.message}`
23+
);
24+
});
25+
}
2126
}
2227

23-
returntrue;
28+
returnvalid;
2429
}

‎tests/validate.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import*asTfrom"../typings/tutorial";
2+
import{validateSchema}from"../src/utils/validate";
3+
4+
describe("validate",()=>{
5+
it("should reject an empty tutorial",()=>{
6+
constjson={version:"here"};
7+
8+
constvalid=validateSchema(json);
9+
expect(valid).toBe(false);
10+
});
11+
it("should return true for a valid tutorial",()=>{
12+
constjson:Partial<T.Tutorial>={
13+
version:"0.1.0",
14+
summary:{title:"Title",description:"Description"},
15+
config:{
16+
testRunner:{
17+
command:"aCommand",
18+
args:{
19+
filter:"filter",
20+
tap:"tap",
21+
},
22+
directory:"coderoad",
23+
setup:{
24+
commits:["abcdef1"],
25+
commands:["npm install"],
26+
},
27+
},
28+
repo:{
29+
uri:"https://github.com/some-repo.git",
30+
branch:"someBranch",
31+
},
32+
dependencies:[{name:"name",version:">=1"}],
33+
appVersions:{
34+
vscode:">=0.7.0",
35+
},
36+
},
37+
levels:[
38+
{
39+
id:"L1",
40+
title:"Level 1",
41+
summary:"The first level",
42+
content:"The first level",
43+
steps:[],
44+
},
45+
],
46+
};
47+
48+
constvalid=validateSchema(json);
49+
expect(valid).toBe(true);
50+
});
51+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp