-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(save): throw validation error if required key is missing
- Loading branch information
1 parent
4ce8d2b
commit 2f1fad5
Showing
3 changed files
with
142 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import "reflect-metadata"; | ||
import joi from 'joi'; | ||
import ValidationError from "./validation-error"; | ||
|
||
const itemParamMetadataKey = Symbol("ItemParam"); | ||
const itemPropertyKey = Symbol("ItemProperty"); | ||
const pkKey = Symbol('PK'); | ||
const skKey = Symbol('SK'); | ||
|
||
export function hashKey(target: any, key: string) { | ||
Check warning on line 10 in src/decorators.ts
|
||
Reflect.defineProperty(target, key, { | ||
get: function () { | ||
return this[pkKey]; | ||
}, | ||
set: function (newVal: string) { | ||
this[pkKey] = newVal | ||
} | ||
}) | ||
} | ||
|
||
export function rangeKey(target: any, key: string) { | ||
Check warning on line 21 in src/decorators.ts
|
||
Reflect.defineProperty(target, key, { | ||
get: function () { | ||
return this[skKey]; | ||
}, | ||
set: function (newVal: string) { | ||
this[skKey] = newVal | ||
} | ||
}) | ||
} | ||
|
||
export function item(target: any, key: string) { | ||
Check warning on line 32 in src/decorators.ts
|
||
Reflect.defineProperty(target, key, { | ||
get: function () { | ||
const pk = this[pkKey] | ||
const sk = this[skKey] | ||
const item = this[itemPropertyKey]; | ||
validateSchema(pk, sk, item); | ||
return item; | ||
}, | ||
set: function (newVal: unknown) { | ||
this[itemPropertyKey] = newVal; | ||
} | ||
}) | ||
} | ||
|
||
export function validItem( | ||
target: any, | ||
Check warning on line 48 in src/decorators.ts
|
||
propertyKey: string, | ||
parameterIndex: number | ||
) { | ||
const existingParameters: number[] = | ||
Reflect.getOwnMetadata(itemParamMetadataKey, target, propertyKey) || []; | ||
existingParameters.push(parameterIndex); | ||
Reflect.defineMetadata( | ||
itemParamMetadataKey, | ||
existingParameters, | ||
target, | ||
propertyKey | ||
); | ||
} | ||
|
||
export function validate( | ||
target: any, | ||
Check warning on line 64 in src/decorators.ts
|
||
propertyName: string, | ||
descriptor: any | ||
Check warning on line 66 in src/decorators.ts
|
||
) { | ||
const original = descriptor.value; | ||
descriptor.value = function (...args: any[]) { | ||
Check warning on line 69 in src/decorators.ts
|
||
const pk = this[pkKey] | ||
const sk = this[skKey] | ||
const parameters: number[] = Reflect.getOwnMetadata( | ||
itemParamMetadataKey, | ||
target, | ||
propertyName | ||
); | ||
if (parameters) { | ||
for (const parameter of parameters) { | ||
validateSchema(pk, sk, args[parameter]); | ||
} | ||
} | ||
return original.apply(this, args); | ||
}; | ||
} | ||
|
||
function validateSchema(pk: string | undefined, sk: string | undefined, item: unknown) { | ||
if (!pk) { | ||
throw new Error('Model error: hash key is not defined on this Model'); | ||
} | ||
const schema = joi.object().keys({ | ||
[pk]: joi.required(), | ||
...(!!sk && { [sk]: joi.required() }) | ||
}).options({ allowUnknown: true }) | ||
const { error } = schema.validate(item); | ||
if (error) { | ||
throw new ValidationError('Validation error', error); | ||
} | ||
} |
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