-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from apiaryio/add-typings
Adds TypeScript type definitions
- Loading branch information
Showing
3 changed files
with
82 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
declare module 'gavel' { | ||
export enum RESTMethods { | ||
GET = 'GET', | ||
POST = 'POST', | ||
PUT = 'PUT', | ||
PATCH = 'PATCH', | ||
DELETE = 'DELETE', | ||
OPTIONS = 'OPTIONS' | ||
} | ||
|
||
export interface HTTPMessage { | ||
method: RESTMethods; | ||
uri: string; | ||
statusCode: string | number; | ||
headers: Record<string, string> | string; | ||
body: Record<string, any> | string; | ||
/** | ||
* [JSON Schema](https://json-schema.org/) Draft V3-4. | ||
*/ | ||
bodySchema: Record<string, any> | string; | ||
} | ||
|
||
export enum FieldKind { | ||
text = 'text', | ||
json = 'json' | ||
} | ||
|
||
export interface ValidationResult { | ||
/** | ||
* Indicates whether the actual HTTP message is valid | ||
* against the expected HTTP message. | ||
*/ | ||
valid: boolean; | ||
/** | ||
* Validation results of each individual HTTP message | ||
* field (i.e. "statusCode", "body", etc). | ||
*/ | ||
fields: Record<string, FieldValidationResult>; | ||
} | ||
|
||
export interface FieldValidationResult { | ||
/** | ||
* Indicates whether a single HTTP message field is valid. | ||
*/ | ||
valid: boolean; | ||
/** | ||
* Kind of validation that has been applied to the field. | ||
*/ | ||
kind: FieldKind | null; | ||
/** | ||
* Normalized HTTP message field values that are being validated. | ||
*/ | ||
values: { | ||
expected: any; | ||
actual: any; | ||
}; | ||
errors: FieldValidationError[]; | ||
} | ||
|
||
export interface FieldValidationError { | ||
message: string; | ||
/** | ||
* Arbitrary information about the validation error. | ||
* Dependends on the HTTP message field's "kind" property. | ||
*/ | ||
location?: { | ||
pointer?: string; | ||
property?: string[]; | ||
}; | ||
} | ||
|
||
/** | ||
* Validates a given expected HTTP message against | ||
* the actual HTTP message. | ||
*/ | ||
export function validate( | ||
expectedMessage: Partial<HTTPMessage>, | ||
actualMessage: Partial<HTTPMessage> | ||
): ValidationResult; | ||
} |