@@ -23,8 +23,9 @@ export type ResourceSchemaValidators = {
23
23
queryValidator : ValidateFunction ;
24
24
} ;
25
25
26
- function initializeAjv ( ) : Ajv {
27
- const removeAdditional = getBooleanFromEnvironment ( 'ALLOW_OVERPOSTING' , false ) ;
26
+ function initializeAjv ( isQueryParameterValidator : boolean ) : Ajv {
27
+ // A query parameter validator cannot have additional properties
28
+ const removeAdditional = isQueryParameterValidator ? false : getBooleanFromEnvironment ( 'ALLOW_OVERPOSTING' , false ) ;
28
29
const coerceTypes = getBooleanFromEnvironment ( 'ALLOW_TYPE_COERCION' , false ) ;
29
30
30
31
const ajv = new Ajv ( { allErrors : true , coerceTypes, removeAdditional } ) ;
@@ -38,14 +39,19 @@ let ajv;
38
39
// simple cache implementation, see: https://rewind.io/blog/simple-caching-in-aws-lambda-functions/
39
40
/** This is a cache mapping MetaEd model objects to compiled ajv JSON Schema validators for the API resource */
40
41
const validatorCache : Map < TopLevelEntity , ResourceSchemaValidators > = new Map ( ) ;
42
+ const queryValidatorCache : Map < TopLevelEntity , ResourceSchemaValidators > = new Map ( ) ;
41
43
42
44
/**
43
45
* Returns the API resource JSON Schema validator functions for the given MetaEd model. Caches results.
44
46
*/
45
- function getSchemaValidatorsFor ( metaEdModel : TopLevelEntity ) : ResourceSchemaValidators {
46
- const cachedValidators : ResourceSchemaValidators | undefined = validatorCache . get ( metaEdModel ) ;
47
+ function getSchemaValidatorsFor (
48
+ metaEdModel : TopLevelEntity ,
49
+ isQueryParameterValidator : boolean = false ,
50
+ ) : ResourceSchemaValidators {
51
+ const validatorCacheObject = isQueryParameterValidator ? queryValidatorCache : validatorCache ;
52
+ const cachedValidators : ResourceSchemaValidators | undefined = validatorCacheObject . get ( metaEdModel ) ;
47
53
if ( cachedValidators != null ) return cachedValidators ;
48
- ajv = initializeAjv ( ) ;
54
+ ajv = initializeAjv ( isQueryParameterValidator ) ;
49
55
const resourceValidators : ResourceSchemaValidators = {
50
56
insertValidator : ajv . compile ( metaEdModel . data . edfiApiSchema . jsonSchemaForInsert ) ,
51
57
updateValidator : ajv . compile ( metaEdModel . data . edfiApiSchema . jsonSchemaForUpdate ) ,
@@ -55,7 +61,7 @@ function getSchemaValidatorsFor(metaEdModel: TopLevelEntity): ResourceSchemaVali
55
61
required : [ ] ,
56
62
} ) ,
57
63
} ;
58
- validatorCache . set ( metaEdModel , resourceValidators ) ;
64
+ validatorCacheObject . set ( metaEdModel , resourceValidators ) ;
59
65
return resourceValidators ;
60
66
}
61
67
@@ -64,6 +70,7 @@ function getSchemaValidatorsFor(metaEdModel: TopLevelEntity): ResourceSchemaVali
64
70
*/
65
71
export function clearAllValidatorCache ( ) : void {
66
72
validatorCache . clear ( ) ;
73
+ queryValidatorCache . clear ( ) ;
67
74
}
68
75
/**
69
76
* Creates a new empty ResourceMatchResult object
@@ -138,7 +145,7 @@ export function validateQueryParametersAgainstSchema(
138
145
metaEdModel : TopLevelEntity ,
139
146
queryParameters : FrontendQueryParameters ,
140
147
) : string [ ] {
141
- const { queryValidator } = getSchemaValidatorsFor ( metaEdModel ) ;
148
+ const { queryValidator } = getSchemaValidatorsFor ( metaEdModel , true ) ;
142
149
143
150
let errors : string [ ] = [ ] ;
144
151
0 commit comments