Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.length as Class property name #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/TypeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export function PropertyCheck(params: PropertyCheckParams = {}): any {
delete params.arrayType;
}

target[propertiesToCheck][target.constructor.name][key] = params;
target[propertiesToCheck][target.constructor.name].push({
key,
params
});
};
}

Expand Down Expand Up @@ -124,9 +127,11 @@ function validateInput(input: any, expectedType: any, arrayType: any = null): an
// If type has propertiesToCheck, it's a complex type with fields to validate
const constructorName = expectedType.constructor.name;
if (expectedType[propertiesToCheck] && expectedType[propertiesToCheck][constructorName]) {
const keysToValidate = Object.keys(expectedType[propertiesToCheck][constructorName]);
for (const key of keysToValidate) {
const checkParams: PropertyCheckParams = expectedType[propertiesToCheck][constructorName][key];
const keysToValidate: string[] = [];
for (const target of expectedType[propertiesToCheck][constructorName]) {
const key = target.key;
const checkParams: PropertyCheckParams = target.params;
keysToValidate.push(key);
// Validate nullable
if (input && input.hasOwnProperty(key)) {
if (!checkParams.nullable && input[key] == null) {
Expand Down
3 changes: 3 additions & 0 deletions tests/TypeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Bar {
public description2: string;
@PropertyCheck({arrayType: {}}) // Invalid array type, items type validation will be disabled
public description3: string[];
@PropertyCheck()
public length: number;
}

class Foo {
Expand Down Expand Up @@ -135,6 +137,7 @@ describe('TypeChecker', () => {
const bar = new Bar();
bar.description = 'Description';
bar.description2 = <any> 3;
bar['length'] = 123;
foo.bar = bar;
expect(() => validate(foo, Foo)).to.throw('bar.description3: Field is required'); // Description2 validation is ignored

Expand Down