Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
style: use prettier and drop Node v4 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
teppeis authored Mar 26, 2018
1 parent 3fdb4d6 commit f5821f1
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 99 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": ["teppeis/node-v4"],
"extends": [
"teppeis/node-v6",
"teppeis/prettier"
],
"rules": {
}
}
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const validateUrl = require('./src/validate-https-url');
* @param {Object=} options
* @return {{valid: boolean, errors: Array<!Object>}} errors is null if valid
*/
module.exports = function(json, options) {
options = options || {};
module.exports = function(json, options = {}) {
let relativePath = () => true;
let maxFileSize = () => true;
if (typeof options.relativePath === 'function') {
Expand Down Expand Up @@ -52,21 +51,23 @@ module.exports = function(json, options) {
const maxBytes = bytes.parse(schema);
const valid = maxFileSize(maxBytes, data);
if (!valid) {
validateMaxFileSize.errors = [{
keyword: 'maxFileSize',
message: `file size should be <= ${schema}`,
params: {
limit: maxBytes,
validateMaxFileSize.errors = [
{
keyword: 'maxFileSize',
message: `file size should be <= ${schema}`,
params: {
limit: maxBytes,
},
},
}];
];
}
return valid;
},
});

const validate = ajv.compile(jsonSchema);
const valid = validate(json);
return {valid: valid, errors: transformErrors(validate.errors)};
return {valid, errors: transformErrors(validate.errors)};
};

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"scripts": {
"gen-dts": "node script/generate-dts.js",
"lint": "eslint index.js src test",
"lint": "eslint index.js src test script",
"mocha": "mocha test --require intelli-espower-loader",
"test": "run-s lint mocha test:dts",
"test:dts": "tsc --noEmit --strict test/manifest-schema.ts"
Expand All @@ -31,6 +31,7 @@
"mocha": "^5.0.5",
"npm-run-all": "^4.1.2",
"power-assert": "^1.4.4",
"prettier": "1.11.1",
"typescript": "^2.7.2"
},
"homepage": "https://github.com/teppeis/kintone-plugin-manifest-validator#readme",
Expand Down
11 changes: 4 additions & 7 deletions script/generate-dts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict';

const fs = require('fs');
const json2ts = require('json-schema-to-typescript');
const {compile} = require('json-schema-to-typescript');
const schema = require('../manifest-schema.json');

const compile = json2ts.compile;
const json = require('../manifest-schema.json');
delete schema.definitions.resources.items.anyOf;

delete json.definitions.resources.items.anyOf;

compile(json, 'manifest-schema.json')
.then(ts => fs.writeFileSync('manifest-schema.d.ts', ts));
compile(schema, 'manifest-schema.json').then(dts => fs.writeFileSync('manifest-schema.d.ts', dts));
150 changes: 81 additions & 69 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,97 @@ describe('validator', () => {
delete manifestJson.version;
assert.deepEqual(validator(manifestJson), {
valid: false,
errors: [{
dataPath: '.version',
keyword: 'required',
message: 'is a required property',
params: {
missingProperty: 'version',
errors: [
{
dataPath: '.version',
keyword: 'required',
message: 'is a required property',
params: {
missingProperty: 'version',
},
schemaPath: '#/required',
},
schemaPath: '#/required',
}],
],
});
});

it('invalid type', () => {
assert.deepEqual(validator(json({version: '1'})), {
valid: false,
errors: [{
dataPath: '.version',
keyword: 'type',
message: 'should be integer',
params: {
type: 'integer',
errors: [
{
dataPath: '.version',
keyword: 'type',
message: 'should be integer',
params: {
type: 'integer',
},
schemaPath: '#/properties/version/type',
},
schemaPath: '#/properties/version/type',
}],
],
});
});

it('integer is out of range', () => {
assert.deepEqual(validator(json({version: 0})), {
valid: false,
errors: [{
dataPath: '.version',
keyword: 'minimum',
message: 'should be >= 1',
params: {
comparison: '>=',
exclusive: false,
limit: 1,
errors: [
{
dataPath: '.version',
keyword: 'minimum',
message: 'should be >= 1',
params: {
comparison: '>=',
exclusive: false,
limit: 1,
},
schemaPath: '#/properties/version/minimum',
},
schemaPath: '#/properties/version/minimum',
}],
],
});
});

it('invalid enum value', () => {
assert.deepEqual(validator(json({type: 'FOO'})), {
valid: false,
errors: [{
dataPath: '.type',
keyword: 'enum',
message: 'should be equal to one of the allowed values',
params: {
allowedValues: [
'APP',
],
errors: [
{
dataPath: '.type',
keyword: 'enum',
message: 'should be equal to one of the allowed values',
params: {
allowedValues: ['APP'],
},
schemaPath: '#/properties/type/enum',
},
schemaPath: '#/properties/type/enum',
}],
],
});
});

it('no English description', () => {
assert.deepEqual(validator(json({description: {}})), {
valid: false,
errors: [{
dataPath: '.description.en',
keyword: 'required',
message: 'is a required property',
params: {
missingProperty: 'en',
errors: [
{
dataPath: '.description.en',
keyword: 'required',
message: 'is a required property',
params: {
missingProperty: 'en',
},
schemaPath: '#/properties/description/required',
},
schemaPath: '#/properties/description/required',
}],
],
});
});

it('2 errors', () => {
const actual = validator(json({
manifest_version: 'a',
version: 0,
}));
const actual = validator(
json({
manifest_version: 'a',
version: 0,
})
);
assert(actual.valid === false);
assert(actual.errors.length === 2);
});
Expand All @@ -110,15 +120,16 @@ describe('validator', () => {
});

it('"http:" is invalid for `https-url`', () => {
const actual = validator(json({
desktop: {
js: [
'http://example.com/icon.png',
],
},
}), {
relativePath: str => !/^https?:/.test(str),
});
const actual = validator(
json({
desktop: {
js: ['http://example.com/icon.png'],
},
}),
{
relativePath: str => !/^https?:/.test(str),
}
);
assert(actual.valid === false);
assert(actual.errors.length === 3);
assert(actual.errors[0].keyword === 'format');
Expand All @@ -129,8 +140,7 @@ describe('validator', () => {
describe('maxFileSize', () => {
it('valid file size', () => {
let called = 0;
const actual = validator(json({
}), {
const actual = validator(json({}), {
maxFileSize(maxFileSizeInBytes, path) {
assert(maxFileSizeInBytes === 524288);
assert(path === 'image/icon.png');
Expand All @@ -143,8 +153,7 @@ describe('validator', () => {
});

it('invalid file size', () => {
const actual = validator(json({
}), {
const actual = validator(json({}), {
maxFileSize(maxFileSizeInBytes, path) {
return false;
},
Expand All @@ -171,13 +180,16 @@ describe('validator', () => {
* @return {!Object}
*/
function json(source) {
return Object.assign({
manifest_version: 1,
version: 1,
type: 'APP',
name: {
en: 'sample plugin',
return Object.assign(
{
manifest_version: 1,
version: 1,
type: 'APP',
name: {
en: 'sample plugin',
},
icon: 'image/icon.png',
},
icon: 'image/icon.png',
}, source);
source
);
}
20 changes: 8 additions & 12 deletions test/validate-https-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ const validate = require('../src/validate-https-url');

describe('validate-https-url', () => {
context('valid', () => {
[
'https://example.com/path/to?foo=bar&baz=piyo#hash',
'https://user:pass@example.com/',
].forEach(url => {
it(url, () => {
assert(validate(url));
});
});
['https://example.com/path/to?foo=bar&baz=piyo#hash', 'https://user:pass@example.com/'].forEach(
url => {
it(url, () => {
assert(validate(url));
});
}
);
});

context('invalid', () => {
Expand All @@ -33,10 +32,7 @@ describe('validate-https-url', () => {

context('`allowHttp`', () => {
context('valid', () => {
[
'https://example.com',
'http://example.com',
].forEach(url => {
['https://example.com', 'http://example.com'].forEach(url => {
it(url, () => {
assert(validate(url, true));
});
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,10 @@ prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"

prettier@1.11.1:
version "1.11.1"
resolved "https://registry.npmjs.org/prettier/-/prettier-1.11.1.tgz#61e43fc4cd44e68f2b0dfc2c38cd4bb0fccdcc75"

prettier@^1.8.2:
version "1.11.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.11.1.tgz#61e43fc4cd44e68f2b0dfc2c38cd4bb0fccdcc75"
Expand Down

0 comments on commit f5821f1

Please sign in to comment.