File tree Expand file tree Collapse file tree 4 files changed +113
-1
lines changed Expand file tree Collapse file tree 4 files changed +113
-1
lines changed Original file line number Diff line number Diff line change 22 "name" : " stac-extensions" ,
33 "version" : " 1.0.0" ,
44 "scripts" : {
5- "test" : " npm run check-markdown && npm run check-examples" ,
5+ "test" : " jest && npm run check-markdown && npm run check-examples" ,
66 "check-markdown" : " remark . -f -r .github/remark.yaml" ,
77 "check-examples" : " stac-node-validator . --lint --verbose --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json" ,
88 "format-examples" : " stac-node-validator . --format --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json"
99 },
10+ "type" : " module" ,
1011 "dependencies" : {
12+ "jest" : " ^27.4.4" ,
1113 "remark-cli" : " ^8.0.0" ,
1214 "remark-lint" : " ^7.0.0" ,
1315 "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 Schemas = new Map ( ) ;
6+ const loadSchema = function ( uri ) {
7+ let existing = Schemas . get ( uri ) ;
8+ if ( existing == null ) {
9+ existing = loadSchemaFromUri ( uri ) ;
10+ Schemas . set ( uri , existing ) ;
11+ }
12+ return existing ;
13+ }
14+
15+ /**
16+ * function passed in to Ajv instance which allows us to load schemas from a url at run time.
17+ */
18+ module . exports . loadSchemaFromUri = async function ( uri ) {
19+ try {
20+ let response = await axios . get ( uri ) ;
21+ return response . data ;
22+ } catch ( error ) {
23+ throw new Error ( `-- Schema at '${ uri } ' not found. Please ensure all entries in 'stac_extensions' are valid.` ) ;
24+ }
25+ }
26+
27+ module . exports . AjvOptions = { loadSchema, formats : Object . assign ( iriFormats ) } ;
28+ module . exports . rootDirectory = dirname ( __dirname ) ;
29+ module . exports . schemaPath = join ( module . exports . rootDirectory , 'json-schema/schema.json' ) ;
You can’t perform that action at this time.
0 commit comments