Skip to content

Commit

Permalink
refactor: precompile schemas, related to stylelint/stylelint#1912 (#19)
Browse files Browse the repository at this point in the history
* refactor: precompile schemas, related to stylelint/stylelint#1912

* test: coverage; schema tests

* test: streamConfig schema

* chore: disable max line length style rule

* style: use camel-case names for variables

* style: silence false-positive no default import

* style: generate file names use camel-case convention

* chore: give script an imperative name

* style: sort scripts alphabetically

* chore: make build script part of the test script

* chore: build assets required to pass linting

* chore: remove ./dist prior to each build

* chore: give script an imperative name

* chore: remove superfluous test

* chore: use nyc to generate code coverage

* chore: give script an imperative name
  • Loading branch information
Evgeny Poberezkin authored and gajus committed Oct 18, 2016
1 parent 1bb1627 commit 76290f6
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 20 deletions.
9 changes: 8 additions & 1 deletion .babelrc
Expand Up @@ -4,5 +4,12 @@
],
"presets": [
"es2015-node4"
]
],
"env": {
"development": {
"plugins": [
"istanbul"
]
}
}
}
5 changes: 4 additions & 1 deletion .eslintrc
@@ -1,3 +1,6 @@
{
"extends": "canonical"
"extends": "canonical",
"rules": {
"max-len": 0
}
}
24 changes: 20 additions & 4 deletions package.json
Expand Up @@ -14,9 +14,12 @@
},
"description": "Formats data into a string table.",
"devDependencies": {
"ajv-cli": "^1.1.0",
"babel": "^6.5.2",
"babel-cli": "^6.14.0",
"babel-core": "^6.14.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-istanbul": "^2.0.3",
"babel-preset-es2015-node4": "^2.1.0",
"babel-register": "^6.14.0",
"chai": "^3.4.1",
Expand All @@ -25,6 +28,7 @@
"gitdown": "^2.4.0",
"husky": "^0.11.7",
"mocha": "^3.0.2",
"nyc": "^8.3.1",
"sinon": "^1.17.2"
},
"keywords": [
Expand All @@ -37,16 +41,28 @@
"license": "BSD-3-Clause",
"main": "./dist/index.js",
"name": "table",
"nyc": {
"include": [
"src/*.js"
],
"instrument": false,
"lines": 70,
"require": [
"babel-register"
],
"sourceMap": false
},
"repository": {
"type": "git",
"url": "https://github.com/gajus/table"
},
"scripts": {
"build": "babel --copy-files ./src --out-dir ./dist",
"lint": "eslint ./src ./tests",
"build": "rm -fr ./dist && babel --copy-files ./src --out-dir ./dist && npm run make-validators",
"lint": "npm run build && eslint ./src ./tests",
"make-readme": "gitdown ./.README/README.md --output-file ./README.md",
"make-validators": "ajv compile --all-errors --inline-refs=false -s src/schemas/config -c ajv-keywords/keywords/typeof -o dist/validateConfig.js && ajv compile --all-errors --inline-refs=false -s src/schemas/streamConfig -c ajv-keywords/keywords/typeof -o dist/validateStreamConfig.js",
"precommit": "npm run lint && npm run test",
"readme": "gitdown ./.README/README.md --output-file ./README.md",
"test": "mocha --compilers js:babel-register"
"test": "npm run build && nyc --check-coverage mocha"
},
"version": "3.8.0"
}
24 changes: 10 additions & 14 deletions src/validateConfig.js
@@ -1,25 +1,21 @@
import Ajv from 'ajv';
import addKeywords from 'ajv-keywords';
import configSchema from './schemas/config.json';
import streamConfigSchema from './schemas/streamConfig.json';
// eslint-disable-next-line import/default
import validateConfig from '../dist/validateConfig';
// eslint-disable-next-line import/default
import validateStreamConfig from '../dist/validateStreamConfig';

const ajv = new Ajv({
allErrors: true
});

addKeywords(ajv, 'typeof');

ajv.addSchema(configSchema);
ajv.addSchema(streamConfigSchema);
const validate = {
'config.json': validateConfig,
'streamConfig.json': validateStreamConfig
};

/**
* @param {string} schemaId
* @param {formatData~config} config
* @returns {undefined}
*/
export default (schemaId, config = {}) => {
if (!ajv.validate(schemaId, config)) {
const errors = ajv.errors.map((error) => {
if (!validate[schemaId](config)) {
const errors = validate[schemaId].errors.map((error) => {
return {
dataPath: error.dataPath,
message: error.message,
Expand Down
43 changes: 43 additions & 0 deletions test/config.js
@@ -0,0 +1,43 @@
import {
expect
} from 'chai';
import configSamples from './configSamples';
import validateConfig from '../dist/validateConfig';
import configSchema from '../src/schemas/config.json';
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';

describe('config.json schema', () => {
var validate;

before(() => {
var ajv = new Ajv({allErrors: true});
ajvKeywords(ajv, 'typeof');
validate = ajv.compile(configSchema);
});

it('should pass validation of valid config samples', () => {
configSamples.valid.forEach((sample, i) => {
testValid(sample, validate);
testValid(sample, validateConfig);
});

function testValid(sample, validate) {
var valid = validate(sample);
if (!valid) console.log(validate.errors);
expect(valid).to.equal(true);
}
});

it('should fail validation of invalid config samples', () => {
configSamples.invalid.forEach((sample, i) => {
testInvalid(sample, validate);
testInvalid(sample, validateConfig);
});

function testInvalid(sample, validate) {
var valid = validate(sample);
expect(valid).to.equal(false);
}
});
});
153 changes: 153 additions & 0 deletions test/configSamples.js
@@ -0,0 +1,153 @@
export default {
valid: [
{
columns: {
0: {
alignment: 'left',
// minWidth: 10,
width: 10
},
1: {
alignment: 'center',
// minWidth: 10,
width: 10
},
2: {
alignment: 'right',
// minWidth: 10,
width: 10
}
}
},
{
border: {
topBody: `─`,
topJoin: `┬`,
topLeft: `┌`,
topRight: `┐`,

bottomBody: `─`,
bottomJoin: `┴`,
bottomLeft: `└`,
bottomRight: `┘`,

bodyLeft: `│`,
bodyRight: `│`,
bodyJoin: `│`,

joinBody: `─`,
joinLeft: `├`,
joinRight: `┤`,
joinJoin: `┼`
}
},
{
columns: {
0: {
paddingLeft: 3
},
1: {
width: 2,
paddingRight: 3
}
}
},
{
border: {},
columnDefault: {
paddingLeft: 0,
paddingRight: 1
},
// drawJoin: () => {
// return false
// }
},
{
columnDefault: {
width: 50
},
// columnCount: 3,
columns: {
0: {
width: 10,
alignment: 'right'
},
1: {
alignment: 'center',
},
2: {
width: 10
}
}
},
{ border: { topBody: '-' } },
{ border: { topJoin: '-' } },
{ border: { topLeft: '-' } },
{ border: { topRight: '-' } },
{ border: { bottomBody: '-' } },
{ border: { bottomJoin: '-' } },
{ border: { bottomLeft: '-' } },
{ border: { bottomRight: '-' } },
{ border: { bodyLeft: '-' } },
{ border: { bodyRight: '-' } },
{ border: { bodyJoin: '-' } },
{ border: { joinBody: '-' } },
{ border: { joinLeft: '-' } },
{ border: { joinRight: '-' } },
{ border: { joinJoin: '-' } },
{ columns: { '1': { alignment: 'left' } } },
{ columns: { '1': { width: 5 } } },
{ columns: { '1': { wrapWord: true } } },
{ columns: { '1': { truncate: 1 } } },
{ columns: { '1': { paddingLeft: 1 } } },
{ columns: { '1': { paddingRight: 1 } } },
{ columnDefault: { alignment: 'left' } },
{ columnDefault: { width: 5 } },
{ columnDefault: { wrapWord: true } },
{ columnDefault: { truncate: 1 } },
{ columnDefault: { paddingLeft: 1 } },
{ columnDefault: { paddingRight: 1 } },
{ drawHorizontalLine: function(){} }
],
invalid: [
{ border: 1 },
{ border: { unknown: '-' } },
{ border: { topBody: 1 } },
{ border: { topJoin: 1 } },
{ border: { topLeft: 1 } },
{ border: { topRight: 1 } },
{ border: { bottomBody: 1 } },
{ border: { bottomJoin: 1 } },
{ border: { bottomLeft: 1 } },
{ border: { bottomRight: 1 } },
{ border: { bodyLeft: 1 } },
{ border: { bodyRight: 1 } },
{ border: { bodyJoin: 1 } },
{ border: { joinBody: 1 } },
{ border: { joinLeft: 1 } },
{ border: { joinRight: 1 } },
{ border: { joinJoin: 1 } },
{ columns: 1 },
{ columns: { a: { width: 5 } } },
{ columns: { '1': 1 } },
{ columns: { '1': { unknown: 1 } } },
{ columns: { '1': { alignment: 1 } } },
{ columns: { '1': { alignment: '1' } } },
{ columns: { '1': { width: '5' } } },
{ columns: { '1': { wrapWord: 1 } } },
{ columns: { '1': { truncate: '1' } } },
{ columns: { '1': { paddingLeft: '1' } } },
{ columns: { '1': { paddingRight: '1' } } },
{ columnDefault: 1 },
{ columnDefault: { unknown: 1 } },
{ columnDefault: { alignment: 1 } },
{ columnDefault: { alignment: '1' } },
{ columnDefault: { width: '5' } },
{ columnDefault: { wrapWord: 1 } },
{ columnDefault: { truncate: '1' } },
{ columnDefault: { paddingLeft: '1' } },
{ columnDefault: { paddingRight: '1' } },
{ drawHorizontalLine: 1 },
{ unknown: 1 }
]
};
43 changes: 43 additions & 0 deletions test/streamConfig.js
@@ -0,0 +1,43 @@
import {
expect
} from 'chai';
import configSamples from './streamConfigSamples';
import validateConfig from '../dist/validateStreamConfig';
import configSchema from '../src/schemas/streamConfig.json';
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';

describe('streamConfig.json schema', () => {
var validate;

before(() => {
var ajv = new Ajv({allErrors: true});
ajvKeywords(ajv, 'typeof');
validate = ajv.compile(configSchema);
});

it('should pass validation of valid streamConfig samples', () => {
configSamples.valid.forEach((sample, i) => {
testValid(sample, validate);
testValid(sample, validateConfig);
});

function testValid(sample, validate) {
var valid = validate(sample);
if (!valid) console.log(validate.errors);
expect(valid).to.equal(true);
}
});

it('should fail validation of invalid streamConfig samples', () => {
configSamples.invalid.forEach((sample, i) => {
testInvalid(sample, validate);
testInvalid(sample, validateConfig);
});

function testInvalid(sample, validate) {
var valid = validate(sample);
expect(valid).to.equal(false);
}
});
});

0 comments on commit 76290f6

Please sign in to comment.