-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isPlainObject } from '@stoplight/json'; | ||
import { createValidator } from './ajv'; | ||
import { convertAjvErrors, RulesetValidationError } from './errors'; | ||
import type { FileRuleDefinition, RuleDefinition, RulesetDefinition } from '../types'; | ||
import AggregateError from 'es-aggregate-error'; | ||
|
||
export function assertValidRuleset( | ||
ruleset: unknown, | ||
format: 'js' | 'json' = 'js', | ||
): asserts ruleset is RulesetDefinition { | ||
if (!isPlainObject(ruleset)) { | ||
throw new RulesetValidationError('Provided ruleset is not an object', []); | ||
} | ||
|
||
if (!('rules' in ruleset) && !('extends' in ruleset) && !('overrides' in ruleset)) { | ||
throw new RulesetValidationError('Ruleset must have rules or extends or overrides defined', []); | ||
} | ||
|
||
const validate = createValidator(format); | ||
|
||
if (!validate(ruleset)) { | ||
throw new AggregateError(convertAjvErrors(validate.errors ?? [])); | ||
} | ||
} | ||
|
||
function isRuleDefinition(rule: FileRuleDefinition): rule is RuleDefinition { | ||
return typeof rule === 'object' && rule !== null && !Array.isArray(rule) && ('given' in rule || 'then' in rule); | ||
} | ||
|
||
export function assertValidRule(rule: FileRuleDefinition, name: string): asserts rule is RuleDefinition { | ||
if (!isRuleDefinition(rule)) { | ||
throw new RulesetValidationError('Rule definition expected', ['rules', name]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,2 @@ | ||
import { isPlainObject } from '@stoplight/json'; | ||
import AggregateError = require('es-aggregate-error'); | ||
|
||
import type { FileRuleDefinition, RuleDefinition, RulesetDefinition } from '../types'; | ||
import { createValidator } from './ajv'; | ||
import { convertAjvErrors } from './errors'; | ||
|
||
import { RulesetValidationError } from './errors'; | ||
|
||
export { RulesetValidationError }; | ||
|
||
export function assertValidRuleset( | ||
ruleset: unknown, | ||
format: 'js' | 'json' = 'js', | ||
): asserts ruleset is RulesetDefinition { | ||
if (!isPlainObject(ruleset)) { | ||
throw new RulesetValidationError('Provided ruleset is not an object', []); | ||
} | ||
|
||
if (!('rules' in ruleset) && !('extends' in ruleset) && !('overrides' in ruleset)) { | ||
throw new RulesetValidationError('Ruleset must have rules or extends or overrides defined', []); | ||
} | ||
|
||
const validate = createValidator(format); | ||
|
||
if (!validate(ruleset)) { | ||
throw new AggregateError(convertAjvErrors(validate.errors ?? [])); | ||
} | ||
} | ||
|
||
function isRuleDefinition(rule: FileRuleDefinition): rule is RuleDefinition { | ||
return typeof rule === 'object' && rule !== null && !Array.isArray(rule) && ('given' in rule || 'then' in rule); | ||
} | ||
|
||
export function assertValidRule(rule: FileRuleDefinition, name: string): asserts rule is RuleDefinition { | ||
if (!isRuleDefinition(rule)) { | ||
throw new RulesetValidationError('Rule definition expected', ['rules', name]); | ||
} | ||
} | ||
export { RulesetValidationError } from './errors'; | ||
export { assertValidRuleset } from './assertions'; |