Skip to content

Commit 87ea833

Browse files
Merge pull request #3 from amio-io/schema-validator
feat: Schema validator
2 parents f9ad09b + 7c36b28 commit 87ea833

12 files changed

+230
-119
lines changed

.circleci/config.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
version: 2.1
3+
4+
defaults: &defaults
5+
working_directory: ~/json-validations-lib
6+
docker:
7+
- image: circleci/node:12.13.1
8+
9+
commands:
10+
npm_install:
11+
description: Installs & caches npm dependencies.
12+
parameters:
13+
cache_version:
14+
type: integer
15+
default: 1
16+
steps:
17+
- restore_cache:
18+
keys:
19+
- v<< parameters.cache_version >>-dependencies-{{ checksum "package.json" }}
20+
# fallback to using the latest cache if no exact match is found
21+
- v<< parameters.cache_version >>-dependencies-
22+
- run: npm install
23+
- save_cache:
24+
paths:
25+
- node_modules
26+
key: v<< parameters.cache_version >>-dependencies-{{ checksum "package.json" }}
27+
28+
jobs:
29+
build:
30+
<<: *defaults
31+
32+
steps:
33+
- checkout
34+
- npm_install:
35+
cache_version: 1
36+
37+
- run:
38+
name: Test - units
39+
command: npm run-script test-units
40+
41+
- persist_to_workspace:
42+
root: ~/json-validations-lib
43+
paths: .
44+
45+
publish_prod:
46+
<<: *defaults
47+
48+
steps:
49+
- attach_workspace:
50+
at: ~/json-validations-lib
51+
- run:
52+
name: Can publish to NPM?
53+
command: npm run-script can-publish-nix
54+
- run:
55+
name: Authenticate with registry
56+
command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/json-validations-lib/.npmrc
57+
- run:
58+
name: Publish package
59+
command: npm publish --access public
60+
61+
workflows:
62+
build_and_publish:
63+
jobs:
64+
- build:
65+
filters:
66+
tags:
67+
# PROD - run CircleCI on tag create
68+
only: /^v[0-9]+(\.[0-9]+)*/
69+
- publish_prod:
70+
requires:
71+
- build
72+
filters:
73+
tags:
74+
# run CircleCI on tag create
75+
only: /^v[0-9]+(\.[0-9]+)*/
76+
branches:
77+
ignore: /.*/

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# json-validations-lib
2-
Private lib. We do not recommend to use it.
2+
We do not recommend to use this lib, it is for Amio purposes.
3+

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
{
22
"name": "json-validations-lib",
3-
"version": "0.0.0",
4-
"private": true,
3+
"version": "1.0.0",
54
"scripts": {
6-
"test-units": "cross-env BUILD_ENV=test DEBUG=amio* mocha test/test_unit/**/*.test.js --reporter mocha-multi-reporters --reporter-options configFile=test/mocha-reporters-config.json"
5+
"test-units": "cross-env BUILD_ENV=test DEBUG=amio* mocha test/test_unit/**/*.test.js --reporter mocha-multi-reporters --reporter-options configFile=test/mocha-reporters-config.json",
6+
"can-publish-nix": "npm info $npm_package_name version | npm-version-bump-checker",
7+
"can-publish-win": "npm info %npm_package_name% version | npm-version-bump-checker"
78
},
89
"dependencies": {
910
"ajv": "6.10.2",
10-
"logzio-node-debug": "^2.0.0",
11+
"logzio-node-debug": "2.0.0",
1112
"ramda": "0.26.1",
1213
"valid-url": "1.0.9"
1314
},
1415
"devDependencies": {
1516
"chai": "4.2.0",
1617
"chai-as-promised": "7.1.1",
18+
"cross-env": "6.0.3",
1719
"mocha": "6.1.4",
1820
"mocha-junit-reporter": "1.22.0",
1921
"mocha-multi-reporters": "1.1.7",
20-
"mochawesome": "3.1.2"
22+
"mochawesome": "3.1.2",
23+
"npm-version-bump-checker": "1.0.4"
2124
}
2225
}

src/async-validator.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
const convertValidationError = require("./utils/validation-errors-converter");
2+
13
class AsyncValidator {
24

35
async validate(data) {
46
throw Error('validate() must be implemented in children')
57
}
68

7-
/** Follows AJV error to be compatible with Schema errors */
9+
/** Follows AJV error to be compatible with Schema errors. */
810
createError(path = [], keyword, data, params) {
9-
return {
11+
return convertValidationError({
1012
dataPath: path.join('.'),
1113
keyword,
1214
data,
1315
params
14-
}
16+
})
1517
}
1618

1719
}

src/errors/schema-validator-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class SchemaValidatorError extends Error{
22
constructor(message, field = undefined, rejectedValue = undefined) {
33
super()
4+
this.message = message
45
this.field = field
56
this.rejected_value = rejectedValue
6-
this.message = message
77
}
88

99
toObject(){

src/utils/validation-errors-converter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function convertValidationError(error) {
88
const params = errorObject.params
99

1010
switch(errorObject.keyword) {
11+
// TODO Create different error messages for objects and strings #4
1112
case 'type':
1213
return new SchemaValidatorError(`Property '${propName}' must be ${params.type}.`, errorObject.dataPath, errorObject.data)
1314
case 'additionalProperties':

test/global-test-hooks.js

Whitespace-only changes.

test/mocha.opts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const chai = require('chai')
2+
const expect = chai.expect
3+
const AsyncValidatorTest = require('../utils/async-validator-test')
4+
chai.use(require('chai-as-promised'))
5+
6+
describe('async-validator test', async () => {
7+
const asyncValidatorTest = new AsyncValidatorTest()
8+
9+
it('Validation passed', async () => {
10+
const result = await asyncValidatorTest.validate({valid: true})
11+
expect(result).to.be.true
12+
})
13+
14+
it('Validation failed', async () => {
15+
const result = asyncValidatorTest.validate({valid: false})
16+
await expect(result).to.be.rejectedWith('expected "valid" to be true').eventually.include({
17+
message: 'expected "valid" to be true',
18+
field: 'valid',
19+
rejected_value: false
20+
})
21+
})
22+
23+
})

test/test_unit/schema-validator.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('Schema Validator', () => {
1818
})
1919

2020
it('Validation failed', () => {
21-
const error = schemaValidator.validate({})
22-
expect(error).to.eql({
21+
const validateFn = schemaValidator.validate.bind(schemaValidator,{})
22+
expect(validateFn).to.throw().to.include({
2323
field: '.',
2424
message: "Missing property '.a'."
2525
})

0 commit comments

Comments
 (0)