Skip to content

Commit

Permalink
feat: use draft-2019-09 (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
annahassel authored Jan 14, 2022
1 parent 8fae4e2 commit c2e78e5
Show file tree
Hide file tree
Showing 9 changed files with 1,493 additions and 1,243 deletions.
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint -e $1
8 changes: 8 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

case "$2,$3" in
merge,)
ex "+%s/Merge branch '\([^']\+\)'/chore(merge): \1/i" -scwq $1 ;;
*) ;;
esac
7 changes: 7 additions & 0 deletions __tests__/fixtures/2019-09.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "array",
"contains": {
"type": "number"
},
"minContains": 2
}
9 changes: 9 additions & 0 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,12 @@ test('throws on invalid URL', () => {
expect(() => validator.ifError<string>('http-url', 'https://'))
.toThrow(HttpStatusError)
})

test('should be able to use 2019-09 schema keywords', () => {
validator = new Validation(CORRECT_PATH)

expect(() => validator.ifError<number[]>('2019-09', [1]))
.toThrow(HttpStatusError)
expect(validator.ifError<number[]>('2019-09', [1, 2]))
.toStrictEqual([1, 2])
})
42 changes: 18 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"compile": "rimraf lib; tsc -b ./tsconfig.build.json",
"prepublishOnly": "yarn compile",
"test": "yarn lint && jest --collectCoverage --runTestsByPath __tests__/**.spec.ts && codecov >/dev/null",
"lint": "eslint --ext .ts -c .eslintrc.js src",
"lint": "eslint --ext .ts -c .eslintrc.js src __tests__",
"semantic-release": "semantic-release"
},
"repository": {
Expand All @@ -28,42 +28,36 @@
},
"homepage": "https://github.com/microfleet/validation#readme",
"devDependencies": {
"@makeomatic/deploy": "^10.4.0",
"@makeomatic/deploy": "^11.0.2",
"@types/callsite": "^1.0.31",
"@types/common-errors": "^1.0.2",
"@types/debug": "^4.1.7",
"@types/glob": "^7.1.4",
"@types/jest": "^27.0.0",
"@types/node": "^16.4.13",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
"@types/glob": "^7.2.0",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"codecov": "^3.8.3",
"eslint": "^7.32.0",
"eslint-config-makeomatic": "^5.0.4",
"jest": "^27.0.6",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
"eslint": "^8.6.0",
"eslint-config-makeomatic": "^5.1.0",
"jest": "^27.4.7",
"ts-jest": "^27.1.2",
"typescript": "^4.5.4"
},
"dependencies": {
"ajv": "^8.6.2",
"ajv-formats": "^2.1.0",
"ajv-keywords": "^5.0.0",
"ajv": "^8.8.2",
"ajv-formats": "^2.1.1",
"ajv-keywords": "^5.1.0",
"callsite": "^1.0.0",
"common-errors": "^1.2.0",
"debug": "^4.3.2",
"glob": "^7.1.7"
"debug": "^4.3.3",
"glob": "^7.2.0"
},
"peerDependencies": {
"common-errors": "~1.x.x"
},
"files": [
"lib/",
"src/"
],
"husky": {
"hooks": {
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS",
"prepare-commit-msg": "./node_modules/@makeomatic/deploy/git-hooks/prepare-commit-msg $HUSKY_GIT_PARAMS"
}
}
]
}
8 changes: 4 additions & 4 deletions src/HttpStatusError.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HttpStatusError as HttpCoreError } from 'common-errors'

export class HttpStatusError extends HttpCoreError {
public errors?: Error[];
public field?: string;
public status?: number;
public status_code?: number;
public errors?: Error[]
public field?: string
public status?: number
public status_code?: number

/**
* @param statusCode
Expand Down
21 changes: 12 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { URL } from 'url'
import Ajv, { ValidateFunction, Options } from 'ajv'
import Ajv, { ValidateFunction, Options } from 'ajv/dist/2019'
import addKeywords from 'ajv-keywords'
import addFormats from 'ajv-formats'
import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json"
import callsite = require('callsite')
import { InvalidOperationError, io, NotFoundError } from 'common-errors'
import _debug = require('debug')
import fs = require('fs')
import { promises as fsAsync } from 'fs'
import glob = require('glob')
import path = require('path')

import { HttpStatusError } from './HttpStatusError'

const debug = _debug('@microfleet/validation')
Expand Down Expand Up @@ -49,12 +51,12 @@ export class Validator {
removeAdditional: false,
useDefaults: true,
verbose: true,
};
}

private readonly schemaDir: string | undefined;
private readonly $ajv: Ajv;
private readonly filterOpt: globFilter;
private readonly schemaOptions: Options;
private readonly schemaDir: string | undefined
private readonly $ajv: Ajv
private readonly filterOpt: globFilter
private readonly schemaOptions: Options

/**
* Initializes validator with schemas in the schemaDir with a given filter function
Expand Down Expand Up @@ -89,6 +91,7 @@ export class Validator {
return false
}
})
ajvInstance.addMetaSchema(draft7MetaSchema)

addKeywords(ajvInstance)
addFormats(ajvInstance)
Expand Down Expand Up @@ -238,7 +241,7 @@ export class Validator {
})
})
} catch (err) {
const error = new io.IOError(`was unable to read ${dir}`, err)
const error = new io.IOError(`was unable to read ${dir}`, err as Error)
throw error
}
}
Expand All @@ -252,7 +255,7 @@ export class Validator {

return glob.sync('**', { cwd: dir })
} catch (err) {
const error = new io.IOError(`was unable to read ${dir}`, err)
const error = new io.IOError(`was unable to read ${dir}`, err as Error)
throw error
}
}
Expand Down Expand Up @@ -305,7 +308,7 @@ export class Validator {
* @param schema - schema name
* @param data
*/
private $validate<T extends unknown = unknown>(schema: string, data: unknown): ValidationResponse<T> {
private $validate<T = unknown>(schema: string, data: unknown): ValidationResponse<T> {
const validate = this.$ajv.getSchema<T>(schema)

if (!validate) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
"esModuleInterop": true,
"resolveJsonModule": true
},
"exclude": [
"**/node_modules/**",
Expand Down
Loading

0 comments on commit c2e78e5

Please sign in to comment.