Skip to content

Commit

Permalink
style: eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jul 22, 2020
1 parent 3025ab2 commit ffbb010
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 28 deletions.
7 changes: 4 additions & 3 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extends: eslint:recommended
parserOptions:
ecmaVersion: 6
env:
node: true
browser: true
Expand All @@ -9,20 +11,19 @@ rules:
curly: [2, multi-or-nest, consistent]
dot-location: [2, property]
dot-notation: 2
indent-legacy: [2, 2, SwitchCase: 1]
linebreak-style: [2, unix]
new-cap: 2
no-console: [2, allow: [warn, error]]
no-else-return: 2
no-eq-null: 2
no-extra-semi: 0
no-fallthrough: 2
no-invalid-this: 2
no-return-assign: 2
no-shadow: 1
no-trailing-spaces: 2
no-use-before-define: [2, nofunc]
quotes: [2, single, avoid-escape]
semi: [2, always]
semi: 0
strict: [2, global]
valid-jsdoc: [2, requireReturn: false]
no-control-regex: 0
Expand Down
9 changes: 0 additions & 9 deletions .jshintrc

This file was deleted.

3 changes: 2 additions & 1 deletion lib/compile/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ function compileAsync(schema, meta, callback) {

function loadMissingSchema(e) {
var ref = e.missingSchema
if (added(ref))
if (added(ref)) {
throw new Error(
"Schema " +
ref +
" is loaded but " +
e.missingRef +
" cannot be resolved"
)
}

var schemaPromise = self._loadingSchemas[ref]
if (!schemaPromise) {
Expand Down
3 changes: 2 additions & 1 deletion lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,11 @@ function compile(schema, root, localRefs, baseId) {
!deps.every(function (keyword) {
return Object.prototype.hasOwnProperty.call(parentSchema, keyword)
})
)
) {
throw new Error(
"parent schema must have all required keywords: " + deps.join(",")
)
}

var validateSchema = rule.definition.validateSchema
if (validateSchema) {
Expand Down
3 changes: 2 additions & 1 deletion lib/compile/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ function resolveIds(schema) {
var id = self._getId(sch)
var baseId = baseIds[parentJsonPtr]
var fullPath = fullPaths[parentJsonPtr] + "/" + parentKeyword
if (keyIndex !== undefined)
if (keyIndex !== undefined) {
fullPath +=
"/" +
(typeof keyIndex == "number" ? keyIndex : util.escapeFragment(keyIndex))
}

if (typeof id == "string") {
id = baseId = normalizeId(baseId ? URI.resolve(baseId, id) : id)
Expand Down
9 changes: 6 additions & 3 deletions lib/compile/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ function checkDataTypes(dataTypes, data, strictNumbers) {
delete types.object
}
if (types.number) delete types.integer
for (var t in types)
for (var t in types) {
code +=
(code ? " && " : "") + checkDataType(t, data, strictNumbers, true)
}

return code
}
Expand Down Expand Up @@ -222,20 +223,22 @@ function getData($data, lvl, paths) {
up = +matches[1]
jsonPointer = matches[2]
if (jsonPointer == "#") {
if (up >= lvl)
if (up >= lvl) {
throw new Error(
"Cannot access property/index " +
up +
" levels up, current level is " +
lvl
)
}
return paths[lvl - up]
}

if (up > lvl)
if (up > lvl) {
throw new Error(
"Cannot access data " + up + " levels up, current level is " + lvl
)
}
data = "data" + (lvl - up || "")
if (!jsonPointer) return data
}
Expand Down
5 changes: 3 additions & 2 deletions lib/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ function validateKeyword(definition, throwError) {

if (v(definition)) return true
validateKeyword.errors = v.errors
if (throwError)
if (throwError) {
throw new Error(
"custom keyword definition is invalid: " + this.errorsText(v.errors)
)
else return false
}
return false
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
],
"scripts": {
"eslint": "eslint lib/{compile/,}*.js spec/{**/,}*.js scripts --ignore-pattern spec/JSON-Schema-Test-Suite",
"jshint": "jshint lib/{compile/,}*.js",
"lint": "npm run jshint && npm run eslint",
"lint": "npm run eslint",
"prettier:write": "prettier --write './**/*.{md,json,yaml,js,ts}'",
"prettier:check": "prettier --list-different './**/*.{md,json,yaml,js,ts}'",
"test-spec": "mocha spec/{**/,}*.spec.js -R spec",
Expand Down
3 changes: 2 additions & 1 deletion spec/async_validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,11 @@ describe("async schemas, formats and keywords", function () {
return repeat(function () {
return Promise.all(
instances.map(function (_ajv) {
if (refSchema)
if (refSchema) {
try {
_ajv.addSchema(refSchema)
} catch (e) {}
}
var validate = _ajv.compile(schema)
var data

Expand Down
14 changes: 9 additions & 5 deletions spec/custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ describe("Custom keywords", function () {
shouldBeValid(validate, {foo: 3.99})

shouldBeInvalid(validate, {foo: 2}, numErrors)
if (customErrors)
if (customErrors) {
shouldBeRangeError(
validate.errors[0],
".foo",
Expand All @@ -914,8 +914,9 @@ describe("Custom keywords", function () {
2,
true
)
}
shouldBeInvalid(validate, {foo: 4}, numErrors)
if (customErrors)
if (customErrors) {
shouldBeRangeError(
validate.errors[0],
".foo",
Expand All @@ -924,6 +925,7 @@ describe("Custom keywords", function () {
4,
true
)
}
})
}

Expand Down Expand Up @@ -982,18 +984,20 @@ describe("Custom keywords", function () {
schema.length == 2 &&
typeof schema[0] == "number" &&
typeof schema[1] == "number"
if (!schemaValid)
if (!schemaValid) {
throw new Error(
"Invalid schema for range keyword, should be array of 2 numbers"
)
}

var exclusiveRangeSchemaValid =
parentSchema.exclusiveRange === undefined ||
typeof parentSchema.exclusiveRange == "boolean"
if (!exclusiveRangeSchemaValid)
if (!exclusiveRangeSchemaValid) {
throw new Error(
"Invalid schema for exclusiveRange keyword, should be bolean"
"Invalid schema for exclusiveRange keyword, should be boolean"
)
}
}

function shouldBeValid(validate, data) {
Expand Down

0 comments on commit ffbb010

Please sign in to comment.