Skip to content

Commit

Permalink
fix: added argsFromObj error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
BlvckBytes committed Nov 26, 2021
1 parent 38b7019 commit 02f9eb4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ctor-ensure",
"author": "BlvckBytes",
"version": "1.0.13",
"version": "1.0.14",
"license": "GPL-3.0",
"repository": {
"type": "git",
Expand Down
11 changes: 9 additions & 2 deletions src/ctor-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { getActiveControls, classRegistry, validateClassCtor } from './ctor-ensu
* @param value Plain object to extract from
* @returns List of arguments in right order
*/
export const argsFromObj = (clazz: Constructable, value: any): any[] => {
export const argsFromObj = (clazz: Constructable, value: any): any[] | null => {
// Get all controls of this class and find the highest validated ctor index
const className = Reflect.getOwnMetadata(META_KEY_DISPLAYNAME, clazz);

// Not a @CtorEnsure class
if (!className) return null;

const ctls = getActiveControls(clazz, className, []);
const maxCtorInd = ctls.reduce((acc, curr) => (curr.ctorInd > acc ? curr.ctorInd : acc), 0);

Expand All @@ -35,6 +39,9 @@ export const validateCtor = (className: string, value: any, templateLang = ''):
const clazz = classRegistry[className]?.clazz;
if (!clazz) return null;

const args = argsFromObj(clazz, value);
if (!args) return null;

// Call validation
return validateClassCtor(className, argsFromObj(clazz, value), templateLang);
return validateClassCtor(className, args, templateLang);
};

0 comments on commit 02f9eb4

Please sign in to comment.