-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
rule.ts
174 lines (133 loc) · 3.93 KB
/
rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ValidationSeverity, ValidationSeverityLabel } from '@stoplight/types/validations';
import { RuleFunction, RuleType } from './enums';
export type Rule =
| IRule
| ITruthyRule
| IOrRule
| IXorRule
| IMaxLengthRule
| IAlphaRule
| INotEndWithRule
| INotContainRule
| IPatternRule
| ISchemaRule
| IParamCheckRule;
export interface IRule<O = any> {
type: RuleType;
// A short summary of the rule and its intended purpose
summary: string;
// The severity of results this rule generates
severity?: ValidationSeverity;
severityLabel?: ValidationSeverityLabel;
// A long-form description of the rule formatted in markdown
description?: string;
// Tags attached to the rule, which can be used for organizational purposes
tags?: string[];
// should the rule be enabled by default?
enabled?: boolean;
// Filter the target down to a subset[] with a JSON path
given: string;
when?: {
// the `path.to.prop` to field, or special `@key` value to target keys for matched `given` object
// EXAMPLE: if the target object is an oas object and given = `$..responses[*]`, then `@key` would be the response code (200, 400, etc)
field: string;
// a regex pattern
pattern?: string;
};
then: {
// the `path.to.prop` to field, or special `@key` value to target keys for matched `given` object
// EXAMPLE: if the target object is an oas object and given = `$..responses[*]`, then `@key` would be the response code (200, 400, etc)
field?: string;
// a regex pattern
pattern?: string;
// name of the function to run
function: string;
// Options passed to the function
functionOptions?: O;
};
}
export interface IRuleParam {
properties: string | string[];
}
export interface IRuleStringParam extends IRuleParam {
value: string;
}
export interface IRuleNumberParam {
value: number;
property?: string;
}
export interface IAlphaRuleParam extends IRuleParam {
// if sorting objects, use key for comparison
keyedBy?: string;
}
export interface IRulePatternParam {
// value to use for rule
value: string;
// object key to apply rule to
property?: string;
// value to omit from regex matching
omit?: string;
// value to split the property on prior to performing regex matching
split?: string;
}
export interface ITruthyRule extends IRule {
function: RuleFunction.TRUTHY;
input: {
// key(s) of object that should evaluate as 'truthy' (considered true in a
// boolean context)
properties: string | string[];
max?: number;
};
}
export interface IOrRule extends IRule {
function: RuleFunction.OR;
input: {
// test to verify if any of the provided keys are present in object
properties: string[];
};
}
export interface IXorRule extends IRule {
function: RuleFunction.XOR;
input: {
// test to verify if one (but not all) of the provided keys are present in
// object
properties: string[];
};
}
export interface IMaxLengthRule extends IRule {
function: RuleFunction.MAX_LENGTH;
// verify property is under a specified number of characters
input: IRuleNumberParam;
}
export interface IAlphaRule extends IRule {
function: RuleFunction.ALPHABETICAL;
// verify property is within alphabetical order
input: IAlphaRuleParam;
}
export interface INotEndWithRule extends IRule {
function: RuleFunction.NOT_END_WITH;
// verify property does not end with string
input: IRulePatternParam;
}
export interface INotContainRule extends IRule {
function: RuleFunction.NOT_CONTAIN;
// verify property does not contain value
input: IRuleStringParam;
}
export interface IPatternRule extends IRule {
function: RuleFunction.PATTERN;
// run regex match
input: IRulePatternParam;
}
export interface ISchemaRule extends IRule {
function: RuleFunction.SCHEMA;
input: {
schema: object;
};
}
export interface IParamCheckRule extends IRule {
function: RuleFunction.SCHEMA;
input: {
schema: object;
};
}