File tree Expand file tree Collapse file tree 4 files changed +115
-1
lines changed Expand file tree Collapse file tree 4 files changed +115
-1
lines changed Original file line number Diff line number Diff line change 2
2
"name" : " stac-extensions" ,
3
3
"version" : " 1.0.0" ,
4
4
"scripts" : {
5
- "test" : " npm run check-markdown && npm run check-examples" ,
5
+ "test" : " jest && npm run check-markdown && npm run check-examples" ,
6
6
"check-markdown" : " remark . -f -r .github/remark.yaml" ,
7
7
"check-examples" : " stac-node-validator . --lint --verbose --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json" ,
8
8
"format-examples" : " stac-node-validator . --format --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json"
9
9
},
10
+ "type" : " module" ,
10
11
"dependencies" : {
12
+ "jest" : " ^27.4.4" ,
11
13
"remark-cli" : " ^8.0.0" ,
12
14
"remark-lint" : " ^7.0.0" ,
13
15
"remark-lint-no-html" : " ^2.0.0" ,
Original file line number Diff line number Diff line change
1
+ const { join } = require ( 'path' ) ;
2
+ const { promises } = require ( 'fs' ) ;
3
+ const { AjvOptions, rootDirectory, schemaPath } = require ( './validation.js' ) ;
4
+ const ajv = new ( require ( 'ajv' ) ) ( AjvOptions ) ;
5
+
6
+ const examplePath = join ( rootDirectory , 'examples/collection.json' ) ;
7
+
8
+ let validate ;
9
+ beforeAll ( async ( ) => {
10
+ const data = JSON . parse ( await promises . readFile ( schemaPath ) ) ;
11
+ validate = await ajv . compileAsync ( data ) ;
12
+ } ) ;
13
+
14
+ describe ( 'Collection example' , ( ) => {
15
+ it ( 'should pass validation' , async ( ) => {
16
+ // given
17
+ const example = JSON . parse ( await promises . readFile ( examplePath ) ) ;
18
+
19
+ // when
20
+ let valid = validate ( example ) ;
21
+
22
+ // then
23
+ expect ( valid ) . toBeTruthy ( ) ;
24
+ } ) ;
25
+
26
+ it ( 'should fail validation without mandatory template:new_field field' , async ( ) => {
27
+ // given
28
+ const example = JSON . parse ( await promises . readFile ( examplePath ) ) ;
29
+ delete example [ 'assets' ] ;
30
+ delete example [ 'item_assets' ] ;
31
+ delete example [ 'template:new_field' ] ;
32
+
33
+ // when
34
+ let valid = validate ( example ) ;
35
+
36
+ // then
37
+ expect ( valid ) . toBeFalsy ( ) ;
38
+ expect (
39
+ validate . errors . some ( ( error ) => error . message === "must have required property 'template:new_field'" ) ,
40
+ ) . toBeTruthy ( ) ;
41
+ } ) ;
42
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const { join } = require ( 'path' ) ;
2
+ const { promises } = require ( 'fs' ) ;
3
+ const { AjvOptions, rootDirectory, schemaPath } = require ( './validation.js' ) ;
4
+ const ajv = new ( require ( 'ajv' ) ) ( AjvOptions ) ;
5
+
6
+ const examplePath = join ( rootDirectory , 'examples/item.json' ) ;
7
+
8
+ let validate ;
9
+ beforeAll ( async ( ) => {
10
+ const data = JSON . parse ( await promises . readFile ( schemaPath ) ) ;
11
+ validate = await ajv . compileAsync ( data ) ;
12
+ } ) ;
13
+
14
+ describe ( 'Item example' , ( ) => {
15
+ it ( 'should pass validation' , async ( ) => {
16
+ // given
17
+ const example = JSON . parse ( await promises . readFile ( examplePath ) ) ;
18
+
19
+ // when
20
+ let valid = validate ( example ) ;
21
+
22
+ // then
23
+ expect ( valid ) . toBeTruthy ( ) ;
24
+ } ) ;
25
+
26
+ it ( 'should fail validation without mandatory template:new_field property ' , async ( ) => {
27
+ // given
28
+ const example = JSON . parse ( await promises . readFile ( examplePath ) ) ;
29
+ delete example . properties [ 'template:new_field' ] ;
30
+ // when
31
+ let valid = validate ( example ) ;
32
+
33
+ // then
34
+ expect ( valid ) . toBeFalsy ( ) ;
35
+ expect (
36
+ validate . errors . some ( ( error ) => error . message === "must have required property 'template:new_field'" ) ,
37
+ ) . toBeTruthy ( ) ;
38
+ } ) ;
39
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const axios = require ( 'axios' ) ;
2
+ const { dirname, join } = require ( 'path' ) ;
3
+ const iriFormats = require ( 'stac-node-validator/iri.js' ) ;
4
+
5
+ // const directory = dirname(fileURLToPath(import.meta.url));
6
+
7
+ const Schemas = new Map ( ) ;
8
+ const loadSchema = function ( uri ) {
9
+ let existing = Schemas . get ( uri ) ;
10
+ if ( existing == null ) {
11
+ existing = loadSchemaFromUri ( uri ) ;
12
+ Schemas . set ( uri , existing ) ;
13
+ }
14
+ return existing ;
15
+ }
16
+
17
+ /**
18
+ * function passed in to Ajv instance which allows us to load schemas from a url at run time.
19
+ */
20
+ module . exports . loadSchemaFromUri = async function ( uri ) {
21
+ try {
22
+ let response = await axios . get ( uri ) ;
23
+ return response . data ;
24
+ } catch ( error ) {
25
+ throw new Error ( `-- Schema at '${ uri } ' not found. Please ensure all entries in 'stac_extensions' are valid.` ) ;
26
+ }
27
+ }
28
+
29
+ module . exports . AjvOptions = { loadSchema, formats : Object . assign ( iriFormats ) } ;
30
+ module . exports . rootDirectory = dirname ( __dirname ) ;
31
+ module . exports . schemaPath = join ( module . exports . rootDirectory , 'json-schema/schema.json' ) ;
You can’t perform that action at this time.
0 commit comments