Skip to content

Commit aaff589

Browse files
committed
Merge branch 'release/0.5.1'
2 parents 842bda8 + a3e1f1d commit aaff589

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,14 @@ const barValidator: Validator<IBar> = extendValidator(fooValidator, {
146146
```
147147

148148
### validate ###
149-
`function validate<T>(arg: any, validator: Validator<T>): Validated<T>`
149+
`function validate<T>(arg: any, validator: Validator<T>, options?: IValidationOptions): Validated<T>`
150150

151151
Checks that `arg` conforms to the type `T` using the given `validator`. Returns an object that conforms to `T` or throws an error.
152152

153+
The third argument is an optional object of options:
154+
155+
- `allowAdditionalProperties: boolean` - If false, an error is thrown if there are any properties in addition to the ones defined in the validator. Defaults to `true`, which removes additional properties from the result.
156+
153157
### optional ###
154158
Used when the property may not present on the object, or its value is undefined. Example:
155159

@@ -422,7 +426,7 @@ validate({
422426

423427
### Handling Validation Errors ###
424428

425-
Errors will always be of the type `ValidationErrorCollection`, which has a property `error: ValidationError[]`.
429+
Errors will always be of the type `ValidationErrorCollection`, which has a property `errors: ValidationError[]`.
426430

427431
The `ValidationError` type has a number of useful properties:
428432

lib/index.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class KeyPathNode extends PathNode {
2222
}
2323

2424
public toString(): string {
25-
if (/^[$a-z_]+$/i.test(this.key)) {
25+
if (/^[$a-z_][$a-z0-9_]*$/i.test(this.key)) {
2626
return `.${this.key}`;
2727
} else {
2828
return `['${this.key.replace('\\', '\\\\').replace("'", "\\'")}']`;
@@ -95,14 +95,30 @@ export class ValidationErrorCollection {
9595
}
9696

9797

98-
export function validate<T>(arg: any, validator: Validator<T>): Validated<T> {
98+
export interface IValidationOptions {
99+
allowAdditionalProperties?: boolean;
100+
}
101+
102+
103+
export function validate<T>(arg: any, validator: Validator<T>, options: IValidationOptions = {}): Validated<T> {
104+
const {
105+
allowAdditionalProperties = true
106+
} = options;
107+
99108
if (typeof arg !== 'object') throw new ValidationErrorCollection(new ValidationError('NOT_OBJECT', `Expected object, got ${typeof arg}`));
100109

101110
const result: {[key in keyof T]?: T[key]} = {};
102111

103112
let validationErrorCollection: ValidationErrorCollection | null = null;
104113

114+
const validatedProperties = Object.keys(arg).reduce((validatedProperties, key) => {
115+
validatedProperties[key] = false;
116+
return validatedProperties;
117+
}, {} as {[key: string]: boolean});
118+
105119
for (const key in validator) {
120+
validatedProperties[key] = true;
121+
106122
try {
107123
result[key] = validator[key](arg[key]);
108124
} catch (err) {
@@ -114,6 +130,16 @@ export function validate<T>(arg: any, validator: Validator<T>): Validated<T> {
114130
}
115131
}
116132

133+
if (!allowAdditionalProperties && !Object.keys(validatedProperties).every(key => validatedProperties[key])) {
134+
if (validationErrorCollection === null) {
135+
validationErrorCollection = new ValidationErrorCollection();
136+
}
137+
138+
validationErrorCollection.errors.push(
139+
new ValidationError('UNEXPECTED_ADDITIONAL_PROPERTIES', `Unexpected additional properties: ${Object.keys(validatedProperties).filter(key => !validatedProperties[key]).join(', ')}`)
140+
);
141+
}
142+
117143
if (validationErrorCollection !== null) throw validationErrorCollection;
118144

119145
return result as Validated<T>;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "validate-interface",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "Validate Objects Against TypeScript Interfaces",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)