Skip to content

Commit

Permalink
Merge pull request #3 from amio-io/schema-validator
Browse files Browse the repository at this point in the history
feat: Schema validator
  • Loading branch information
javiertrancoso authored Dec 16, 2019
2 parents f9ad09b + 7c36b28 commit 87ea833
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 119 deletions.
77 changes: 77 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

version: 2.1

defaults: &defaults
working_directory: ~/json-validations-lib
docker:
- image: circleci/node:12.13.1

commands:
npm_install:
description: Installs & caches npm dependencies.
parameters:
cache_version:
type: integer
default: 1
steps:
- restore_cache:
keys:
- v<< parameters.cache_version >>-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v<< parameters.cache_version >>-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v<< parameters.cache_version >>-dependencies-{{ checksum "package.json" }}

jobs:
build:
<<: *defaults

steps:
- checkout
- npm_install:
cache_version: 1

- run:
name: Test - units
command: npm run-script test-units

- persist_to_workspace:
root: ~/json-validations-lib
paths: .

publish_prod:
<<: *defaults

steps:
- attach_workspace:
at: ~/json-validations-lib
- run:
name: Can publish to NPM?
command: npm run-script can-publish-nix
- run:
name: Authenticate with registry
command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/json-validations-lib/.npmrc
- run:
name: Publish package
command: npm publish --access public

workflows:
build_and_publish:
jobs:
- build:
filters:
tags:
# PROD - run CircleCI on tag create
only: /^v[0-9]+(\.[0-9]+)*/
- publish_prod:
requires:
- build
filters:
tags:
# run CircleCI on tag create
only: /^v[0-9]+(\.[0-9]+)*/
branches:
ignore: /.*/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# json-validations-lib
Private lib. We do not recommend to use it.
We do not recommend to use this lib, it is for Amio purposes.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
{
"name": "json-validations-lib",
"version": "0.0.0",
"private": true,
"version": "1.0.0",
"scripts": {
"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"
"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",
"can-publish-nix": "npm info $npm_package_name version | npm-version-bump-checker",
"can-publish-win": "npm info %npm_package_name% version | npm-version-bump-checker"
},
"dependencies": {
"ajv": "6.10.2",
"logzio-node-debug": "^2.0.0",
"logzio-node-debug": "2.0.0",
"ramda": "0.26.1",
"valid-url": "1.0.9"
},
"devDependencies": {
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"cross-env": "6.0.3",
"mocha": "6.1.4",
"mocha-junit-reporter": "1.22.0",
"mocha-multi-reporters": "1.1.7",
"mochawesome": "3.1.2"
"mochawesome": "3.1.2",
"npm-version-bump-checker": "1.0.4"
}
}
8 changes: 5 additions & 3 deletions src/async-validator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const convertValidationError = require("./utils/validation-errors-converter");

class AsyncValidator {

async validate(data) {
throw Error('validate() must be implemented in children')
}

/** Follows AJV error to be compatible with Schema errors */
/** Follows AJV error to be compatible with Schema errors. */
createError(path = [], keyword, data, params) {
return {
return convertValidationError({
dataPath: path.join('.'),
keyword,
data,
params
}
})
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/errors/schema-validator-error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class SchemaValidatorError extends Error{
constructor(message, field = undefined, rejectedValue = undefined) {
super()
this.message = message
this.field = field
this.rejected_value = rejectedValue
this.message = message
}

toObject(){
Expand Down
1 change: 1 addition & 0 deletions src/utils/validation-errors-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function convertValidationError(error) {
const params = errorObject.params

switch(errorObject.keyword) {
// TODO Create different error messages for objects and strings #4
case 'type':
return new SchemaValidatorError(`Property '${propName}' must be ${params.type}.`, errorObject.dataPath, errorObject.data)
case 'additionalProperties':
Expand Down
Empty file removed test/global-test-hooks.js
Empty file.
3 changes: 0 additions & 3 deletions test/mocha.opts

This file was deleted.

23 changes: 23 additions & 0 deletions test/test_unit/async-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const chai = require('chai')
const expect = chai.expect
const AsyncValidatorTest = require('../utils/async-validator-test')
chai.use(require('chai-as-promised'))

describe('async-validator test', async () => {
const asyncValidatorTest = new AsyncValidatorTest()

it('Validation passed', async () => {
const result = await asyncValidatorTest.validate({valid: true})
expect(result).to.be.true
})

it('Validation failed', async () => {
const result = asyncValidatorTest.validate({valid: false})
await expect(result).to.be.rejectedWith('expected "valid" to be true').eventually.include({
message: 'expected "valid" to be true',
field: 'valid',
rejected_value: false
})
})

})
4 changes: 2 additions & 2 deletions test/test_unit/schema-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('Schema Validator', () => {
})

it('Validation failed', () => {
const error = schemaValidator.validate({})
expect(error).to.eql({
const validateFn = schemaValidator.validate.bind(schemaValidator,{})
expect(validateFn).to.throw().to.include({
field: '.',
message: "Missing property '.a'."
})
Expand Down
Loading

0 comments on commit 87ea833

Please sign in to comment.