1
+ import ajvFormats from 'ajv-formats' ;
1
2
import ld from 'lodash' ;
2
-
3
- import { MimeTypeRegistry } from './utils/mime' ;
4
- import TextBodyParser from './bodyParsers/TextBodyParser' ;
5
- import JsonBodyParser from './bodyParsers/JsonBodyParser' ;
6
3
import BodyParserWrapper from './bodyParsers/BodyParserWrapper' ;
4
+ import JsonBodyParser from './bodyParsers/JsonBodyParser' ;
5
+ import TextBodyParser from './bodyParsers/TextBodyParser' ;
7
6
import { loadControllersSync } from './controllers/loadControllers' ;
8
-
9
7
import {
10
- CustomFormats ,
11
- ExegesisOptions ,
12
- StringParser ,
8
+ Authenticators ,
13
9
BodyParser ,
14
10
Controllers ,
15
- Authenticators ,
11
+ CustomFormats ,
12
+ ExegesisOptions ,
16
13
MimeTypeParser ,
17
14
ResponseValidationCallback ,
15
+ StringParser ,
18
16
} from './types' ;
19
- import { HandleErrorFunction } from './types/options' ;
17
+ import {
18
+ HandleErrorFunction ,
19
+ NumberCustomFormatChecker ,
20
+ StringCustomFormatChecker ,
21
+ CustomFormatChecker ,
22
+ } from './types/options' ;
23
+ import { MimeTypeRegistry } from './utils/mime' ;
20
24
21
25
export interface ExegesisCompiledOptions {
22
26
customFormats : CustomFormats ;
@@ -34,50 +38,28 @@ export interface ExegesisCompiledOptions {
34
38
treatReturnedJsonAsPure : boolean ;
35
39
}
36
40
37
- const INT_32_MIN = - 1 * Math . pow ( 2 , 31 ) ;
38
- const INT_32_MAX = Math . pow ( 2 , 31 ) - 1 ;
39
- // Javascript can only safely support a range of -(2^53 - 1) to (2^53 - 1)
40
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
41
- const INT_64_MIN = Number . MIN_SAFE_INTEGER ;
42
- const INT_64_MAX = Number . MAX_SAFE_INTEGER ;
43
-
44
41
// See the OAS 3.0 specification for full details about supported formats:
45
42
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#data-types
46
43
const defaultValidators : CustomFormats = {
47
- // string:date is taken care of for us:
48
- // https://github.com/epoberezkin/ajv/blob/797dfc8c2b0f51aaa405342916cccb5962dd5f21/lib/compile/formats.js#L34
49
- // string:date-time is from:
50
- // https://tools.ietf.org/html/draft-wright-json-schema-validation-00#section-7.3.1.
51
- // number:int32 and number:int64 are defined as non-fractional integers
52
- // https://tools.ietf.org/html/draft-wright-json-schema-00#section-5.3
53
- int32 : {
54
- type : 'number' ,
55
- validate : ( value : number ) => value >= INT_32_MIN && value <= INT_32_MAX ,
56
- } ,
57
- int64 : {
58
- type : 'number' ,
59
- validate : ( value : number ) => value >= INT_64_MIN && value <= INT_64_MAX ,
60
- } ,
61
- double : {
62
- type : 'number' ,
63
- validate : ( ) => true ,
64
- } ,
65
- float : {
66
- type : 'number' ,
67
- validate : ( ) => true ,
68
- } ,
44
+ // TODO: Support async validators so we don't need all this casting.
45
+ int32 : ajvFormats . get ( 'int32' ) as NumberCustomFormatChecker ,
46
+ int64 : ajvFormats . get ( 'int64' ) as NumberCustomFormatChecker ,
47
+ double : ajvFormats . get ( 'double' ) as NumberCustomFormatChecker ,
48
+ float : ajvFormats . get ( 'float' ) as NumberCustomFormatChecker ,
69
49
// Nothing to do for 'password'; this is just a hint for docs.
70
50
password : ( ) => true ,
71
51
// Impossible to validate "binary".
72
52
binary : ( ) => true ,
73
- // `byte` is base64 encoded data. We *could* validate it here, but if the
74
- // string is long, we might take a while to do it, and the application will
75
- // figure it out quickly enough when it tries to decode it, so we just
76
- // pass it along.
77
- byte : ( ) => true ,
53
+ byte : ajvFormats . get ( 'byte' ) as RegExp ,
78
54
// Not defined by OAS 3, but it's used throughout OAS 3.0.1, so we put it
79
55
// here as an alias for 'byte' just in case.
80
- base64 : ( ) => true ,
56
+ base64 : ajvFormats . get ( 'byte' ) as RegExp ,
57
+ // Various formats we're supposed to support per the JSON Schema RFC.
58
+ // https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.3
59
+ date : ajvFormats . get ( 'date' ) as CustomFormatChecker ,
60
+ time : ajvFormats . get ( 'time' ) as StringCustomFormatChecker ,
61
+ 'date-time' : ajvFormats . get ( 'date-time' ) as StringCustomFormatChecker ,
62
+ duration : ajvFormats . get ( 'duration' ) as RegExp ,
81
63
} ;
82
64
83
65
export function compileOptions ( options : ExegesisOptions = { } ) : ExegesisCompiledOptions {
0 commit comments