Skip to content

Commit

Permalink
fix(schema): ensure the schemas are 'required()'
Browse files Browse the repository at this point in the history
fixes #257
  • Loading branch information
tripodsan committed Mar 2, 2020
1 parent 6455a3b commit d89bcff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
13 changes: 9 additions & 4 deletions src/IndexConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
const SchemaDerivedConfig = require('./SchemaDerivedConfig.js');
const { NamedMapHandler } = require('./NamedMapHandler');

const indexConfigSchema = require('./schemas/indexconfig.schema.json');
const indexSchema = require('./schemas/index.schema.json');
const propertySchema = require('./schemas/property.schema.json');
const querySchema = require('./schemas/query.schema.json');

class IndexConfig extends SchemaDerivedConfig {
constructor() {
super({
filename: 'helix-query.yaml',
schemas: {
'^/$': 'indexconfig.schema.json',
'^/indices/.*$': 'index.schema.json',
'^/indices/.*/properties/.*$': 'property.schema.json',
'^/indices/.*/queries/.*$': 'query.schema.json',
'^/$': indexConfigSchema,
'^/indices/.*$': indexSchema,
'^/indices/.*/properties/.*$': propertySchema,
'^/indices/.*/queries/.*$': querySchema,
},
handlers: {
'^/indices$': NamedMapHandler(),
Expand Down
10 changes: 7 additions & 3 deletions src/MarkupConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
const SchemaDerivedConfig = require('./SchemaDerivedConfig.js');
const { NamedMapHandler } = require('./NamedMapHandler');

const markupConfigSchema = require('./schemas/markupconfig.schema.json');
const markupSchema = require('./schemas/markup.schema.json');
const markupMappingSchema = require('./schemas/markupmapping.schema.json');

class MarkupConfig extends SchemaDerivedConfig {
constructor() {
super({
filename: 'helix-markup.yaml',
schemas: {
'^/$': 'markupconfig.schema.json',
'^/markup$': 'markup.schema.json',
'^/markup/.*$': 'markupmapping.schema.json',
'^/$': markupConfigSchema,
'^/markup$': markupSchema,
'^/markup/.*$': markupMappingSchema,
},
handlers: {
'^/markup$': NamedMapHandler(),
Expand Down
7 changes: 5 additions & 2 deletions src/MountConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
const SchemaDerivedConfig = require('./SchemaDerivedConfig.js');
const { MountPointHandler } = require('./MountPointHandler');

const fstabSchema = require('./schemas/fstab.schema.json');
const mountpointSchema = require('./schemas/mountpoint.schema.json');

const onedriveDecorator = {
test(m) {
return /https:\/\/.*\.sharepoint\.com/.test(m.url) || m.url.startsWith('https://1drv.ms/');
Expand Down Expand Up @@ -42,8 +45,8 @@ class MountConfig extends SchemaDerivedConfig {
super({
filename: 'fstab.yaml',
schemas: {
'^/$': 'fstab.schema.json',
'^/mountpoints/.*$': 'mountpoint.schema.json',
'^/$': fstabSchema,
'^/mountpoints/.*$': mountpointSchema,
},
handlers: {
'^/mountpoints$': MountPointHandler([onedriveDecorator, googleDecorator]),
Expand Down
15 changes: 2 additions & 13 deletions src/SchemaDerivedConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const fs = require('fs-extra');
const path = require('path');
const Ajv = require('ajv');
const BaseConfig = require('./BaseConfig.js');

Expand All @@ -22,7 +20,7 @@ class SchemaDerivedConfig extends BaseConfig {
*
* @param {object} opts
* @param {string} opts.filename - the source file when loading the config from disk
* @param {object} opts.schema - a mapping between JSON paths (regex) and schema file names
* @param {object} opts.schemas - a mapping between JSON paths (regex) and schema file names
* @param {object} opts.handlers - a mapping between JSON paths (regex) and proxy handlers
*/
constructor({
Expand Down Expand Up @@ -60,10 +58,7 @@ class SchemaDerivedConfig extends BaseConfig {
coerceTypes: 'array',
});

for (const value of Object.values(this._schemas)) {
ajv.addSchema(value);
}

ajv.addSchema(Object.values(this._schemas));
const res = ajv.validate(this._schemas['^/$'], this._cfg);
if (res) {
return res;
Expand Down Expand Up @@ -129,12 +124,6 @@ class SchemaDerivedConfig extends BaseConfig {
*/
async init() {
await this.loadConfig();

for (const [key, value] of Object.entries(this._schemas)) {
const schema = fs.readJsonSync(path.resolve(__dirname, 'schemas', value));
this._schemas[key] = schema;
}

await this.validate();

this._content = new Proxy(this._cfg, this.defaultHandler(''));
Expand Down

0 comments on commit d89bcff

Please sign in to comment.