Skip to content

Commit

Permalink
feat(core): support JSON ruleset validation (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Apr 14, 2022
1 parent 4ececf0 commit aeb7d5b
Show file tree
Hide file tree
Showing 25 changed files with 501 additions and 293 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"lint": "yarn lint.prettier && yarn lint.eslint",
"lint.fix": "yarn lint.prettier --write && yarn lint.eslint --fix",
"lint.eslint": "eslint --cache --cache-location .cache/.eslintcache --ext=.js,.mjs,.ts packages test-harness",
"lint.prettier": "prettier --ignore-path .eslintignore --ignore-unknown --check packages/core/src/meta/*.json packages/rulesets/src/{asyncapi,oas}/schemas/*.json docs/**/*.md README.md",
"lint.prettier": "prettier --ignore-path .eslintignore --ignore-unknown --check packages/core/src/ruleset/meta/*.json packages/rulesets/src/{asyncapi,oas}/schemas/*.json docs/**/*.md README.md",
"pretest": "yarn workspace @stoplight/spectral-ruleset-migrator pretest",
"test": "yarn pretest && yarn test.karma && yarn test.jest",
"test.harness": "jest -c ./test-harness/jest.config.js",
Expand Down Expand Up @@ -116,7 +116,7 @@
"README.md": [
"prettier --write"
],
"packages/core/src/meta/*.json": [
"packages/core/src/ruleset/meta/*.json": [
"prettier --ignore-path .eslintignore --write"
],
"packages/rulesets/src/{asyncapi,oas}/schemas/*.json": [
Expand Down
15 changes: 13 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"name": "@stoplight/spectral-core",
"version": "1.11.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"homepage": "https://github.com/stoplightio/spectral",
"bugs": "https://github.com/stoplightio/spectral/issues",
Expand All @@ -15,6 +13,19 @@
"files": [
"dist"
],
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js"
},
"./ruleset/validation": {
"types": "./dist/ruleset/validation/index.d.ts",
"require": "./dist/ruleset/validation/index.js"
}
},
"engines": {
"node": "^12.20 || >= 14.13"
},
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions packages/core/src/ruleset/__tests__/__fixtures__/foo-ruleset.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/ruleset/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DiagnosticSeverity } from '@stoplight/types';
import { Ruleset } from '../ruleset';
import { RulesetDefinition } from '../types';
import { print } from './__helpers__/print';
import { RulesetValidationError } from '../validation';
import { RulesetValidationError } from '../validation/index';
import { isPlainObject } from '@stoplight/json';
import { Format } from '../format';
import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/ruleset/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { assertValidRuleset, RulesetValidationError } from './validation';
export { assertValidRuleset, RulesetValidationError } from './validation/index';
export { getDiagnosticSeverity } from './utils';
export { createRulesetFunction, SchemaDefinition as RulesetFunctionSchemaDefinition } from './rulesetFunction';
export { Format } from './format';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/ruleset/mergers/rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Optional } from '@stoplight/types';
import { assertValidRule } from '../validation';
import { assertValidRule } from '../validation/assertions';
import { Rule } from '../rule';
import type { Ruleset } from '../ruleset';
import { FileRuleDefinition } from '../types';
Expand Down
64 changes: 64 additions & 0 deletions packages/core/src/ruleset/meta/js-extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "@stoplight/spectral-core/meta/extensions",
"$defs": {
"Extends": {
"$anchor": "extends",
"oneOf": [
{
"$id": "ruleset",
"$ref": "ruleset.schema#",
"errorMessage": "must be a valid ruleset"
},
{
"type": "array",
"items": {
"anyOf": [
{
"$ref": "ruleset"
},
{
"type": "array",
"minItems": 2,
"additionalItems": false,
"items": [
{
"$ref": "ruleset"
},
{
"type": "string",
"enum": ["off", "recommended", "all"],
"errorMessage": "allowed types are \"off\", \"recommended\" and \"all\""
}
]
}
]
}
}
],
"errorMessage": "must be a valid ruleset"
},
"Format": {
"$anchor": "format",
"spectral-runtime": "format",
"errorMessage": "must be a valid format"
},
"Function": {
"$anchor": "function",
"spectral-runtime": "function",
"type": "object",
"properties": {
"function": true
},
"required": ["function"]
},
"Functions": {
"$anchor": "functions",
"not": {}
},
"FunctionsDir": {
"$anchor": "functionsDir",
"not": {}
}
}
}
79 changes: 79 additions & 0 deletions packages/core/src/ruleset/meta/json-extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "@stoplight/spectral-core/meta/extensions",
"$defs": {
"Extends": {
"$anchor": "extends",
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"minItems": 2,
"additionalItems": false,
"items": [
{
"type": "string"
},
{
"enum": ["all", "recommended", "off"],
"errorMessage": "allowed types are \"off\", \"recommended\" and \"all\""
}
]
}
]
}
}
]
},
"Format": {
"$anchor": "format",
"enum": [
"oas2",
"oas3",
"oas3.0",
"oas3.1",
"asyncapi2",
"json-schema",
"json-schema-loose",
"json-schema-draft4",
"json-schema-draft6",
"json-schema-draft7",
"json-schema-draft-2019-09",
"json-schema-2019-09",
"json-schema-draft-2020-12",
"json-schema-2020-12"
],
"errorMessage": "must be a valid format"
},
"Functions": {
"$anchor": "functions",
"type": "array",
"items": {
"type": "string"
}
},
"FunctionsDir": {
"$anchor": "functionsDir",
"type": "string"
},
"Function": {
"$anchor": "function",
"type": "object",
"properties": {
"function": {
"type": "string"
}
},
"required": ["function"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
}
},
{
"type": "object",
"spectral-runtime": "ruleset-function",
"required": ["function"]
"$ref": "extensions#function"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,26 @@
"format": "url",
"errorMessage": "must be a valid URL"
},
"description": { "type": "string" },
"description": {
"type": "string"
},
"rules": {
"type": "object",
"additionalProperties": {
"$ref": "rule.schema#"
}
},
"functions": {
"$ref": "extensions#functions"
},
"functionsDir": {
"$ref": "extensions#functionsDir"
},
"formats": {
"$ref": "shared#formats"
},
"extends": {
"oneOf": [
{
"$ref": "#",
"errorMessage": "must be a valid ruleset"
},
{
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/properties/extends/oneOf/0"
},
{
"type": "array",
"minItems": 2,
"additionalItems": false,
"items": [
{
"$ref": "#"
},
{
"type": "string",
"enum": ["off", "recommended", "all"],
"errorMessage": "allowed types are \"off\", \"recommended\" and \"all\""
}
]
}
]
}
}
],
"errorMessage": "must be a valid ruleset"
"$ref": "extensions#extends"
},
"parserOptions": {
"type": "object",
Expand Down
Loading

0 comments on commit aeb7d5b

Please sign in to comment.