Skip to content

Commit

Permalink
removed async nature of validation as there is basically no need.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Mar 19, 2019
1 parent da56b8c commit f9fe626
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -199,7 +199,7 @@ You can also custom validators
import {Field, AddValidator, PropertyValidator, PropertyValidatorError, ClassType} from '@marcj/marshal';
class MyCustomValidator implements PropertyValidator {
async validate<T>(value: any, target: ClassType<T>, propertyName: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError {
if (value.length > 10) {
return new PropertyValidatorError('Too long :()');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/decorators.ts
Expand Up @@ -10,7 +10,7 @@ import {Buffer} from "buffer";
export const RegisteredEntities: { [name: string]: ClassType<any> } = {};

export interface PropertyValidator {
validate<T>(value: any, target: ClassType<T>, propertyName: string): Promise<PropertyValidatorError | void>;
validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError | void;
}

type IndexOptions = Partial<{
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/mapper.ts
Expand Up @@ -633,8 +633,6 @@ export function plainToClass<T>(
/**
* Same as [plainToClass] but with validation before creating the class instance.
*
* Note: this is a async function since validators are async per default.
*
* ```typescript
* try {
* const entity = await validatedPlainToClass(MyEntity, {field1: 'value'});
Expand All @@ -646,12 +644,12 @@ export function plainToClass<T>(
* }
* ```
*/
export async function validatedPlainToClass<T>(
export function validatedPlainToClass<T>(
classType: ClassType<T>,
data: object,
parents?: any[]
): Promise<T> {
const errors = await validate(classType, data);
): T {
const errors = validate(classType, data);
if (errors.length) {
throw new ValidationFailed(errors);
}
Expand Down
35 changes: 16 additions & 19 deletions packages/core/src/validation.ts
Expand Up @@ -22,7 +22,7 @@ export class PropertyValidatorError {
* import {PropertyValidator} from '@marcj/marshal';
*
* class MyCustomValidator implements PropertyValidator {
* async validate<T>(value: any, target: ClassType<T>, propertyName: string): Promise<PropertyValidatorError | void> {
* async validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError | void {
* if (value.length > 10) {
* return new PropertyValidatorError('Too long :()');
* }
Expand Down Expand Up @@ -62,15 +62,12 @@ export function AddValidator<T extends PropertyValidator>(validator: ClassType<T
* ```
* @category Decorator
*/
export function InlineValidator<T extends PropertyValidator>(cb: (value: any, target: ClassType<any>, propertyName: string) => void | Promise<void>) {
export function InlineValidator<T extends PropertyValidator>(cb: (value: any, target: ClassType<any>, propertyName: string) => PropertyValidatorError | void) {
return (target: Object, property: string) => {
addValidator(target, property, class implements PropertyValidator {
async validate<T>(value: any, target: ClassType<T>, propertyName: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError | void {
try {
const res = cb(value, target, propertyName);
if (res['then']) {
await res;
}
return cb(value, target, propertyName);
} catch (error) {
return new PropertyValidatorError(error.message ? error.message : error);
}
Expand All @@ -83,7 +80,7 @@ export function InlineValidator<T extends PropertyValidator>(cb: (value: any, ta
* @hidden
*/
export class NumberValidator implements PropertyValidator {
async validate<T>(value: any, target: Object, property: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: Object, property: string): PropertyValidatorError | void {
value = parseFloat(value);

if (!Number.isFinite(value)) {
Expand All @@ -96,7 +93,7 @@ export class NumberValidator implements PropertyValidator {
* @hidden
*/
export class StringValidator implements PropertyValidator {
async validate<T>(value: any, target: Object, property: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: Object, property: string): PropertyValidatorError | void {
if ('string' !== typeof value) {
return new PropertyValidatorError('No String given');
}
Expand All @@ -107,7 +104,7 @@ export class StringValidator implements PropertyValidator {
* @hidden
*/
export class DateValidator implements PropertyValidator {
async validate<T>(value: any, target: Object, property: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: Object, property: string): PropertyValidatorError | void {
if (value instanceof Date) {
if (isNaN(new Date(value).getTime())) {
return new PropertyValidatorError('No valid Date given');
Expand All @@ -130,7 +127,7 @@ export class DateValidator implements PropertyValidator {
* @hidden
*/
export class RequiredValidator implements PropertyValidator {
async validate<T>(value: any, target: ClassType<T>, propertyName: string): Promise<PropertyValidatorError | void> {
validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError | void {
if (undefined === value) {
return new PropertyValidatorError('Required value is undefined');
}
Expand Down Expand Up @@ -195,7 +192,7 @@ export class ValidationFailed {
* validate(SimpleModel, {id: false});
* ```
*/
export async function validate<T>(classType: ClassType<T>, item: { [name: string]: any } | T, path?: string): Promise<ValidationError[]> {
export function validate<T>(classType: ClassType<T>, item: { [name: string]: any } | T, path?: string): ValidationError[] {
const properties = getRegisteredProperties(classType);
const errors: ValidationError[] = [];
const schema = getEntitySchema(classType);
Expand All @@ -204,14 +201,14 @@ export async function validate<T>(classType: ClassType<T>, item: { [name: string
item = applyDefaultValues(classType, item as object);
}

async function handleValidator(
function handleValidator(
validatorType: ClassType<PropertyValidator>,
value: any,
propertyName: string,
propertyPath: string
): Promise<boolean> {
): boolean {
const instance = new validatorType;
const result = await instance.validate(value, classType, propertyName);
const result = instance.validate(value, classType, propertyName);
if (result instanceof PropertyValidatorError) {
errors.push(new ValidationError(propertyPath, result.message));
return true;
Expand All @@ -228,7 +225,7 @@ export async function validate<T>(classType: ClassType<T>, item: { [name: string
const propertyValue: any = item[propertyName];

if (!isOptional(classType, propertyName)) {
if (await handleValidator(RequiredValidator, propertyValue, propertyName, propertyPath)) {
if (handleValidator(RequiredValidator, propertyValue, propertyName, propertyPath)) {
//there's no need to continue validation without a value.
continue;
}
Expand All @@ -254,7 +251,7 @@ export async function validate<T>(classType: ClassType<T>, item: { [name: string
}

for (const validatorType of validators) {
await handleValidator(validatorType, propertyValue, propertyName, propertyPath);
handleValidator(validatorType, propertyValue, propertyName, propertyPath);
}

if (propSchema.type === 'class') {
Expand All @@ -264,11 +261,11 @@ export async function validate<T>(classType: ClassType<T>, item: { [name: string

for (const i in propertyValue) {
const deepPropertyPath = propertyPath + '.' + i;
errors.push(...await validate(propSchema.getResolvedClassType(), propertyValue[i], deepPropertyPath));
errors.push(...validate(propSchema.getResolvedClassType(), propertyValue[i], deepPropertyPath));
}
} else {
//deep validation
errors.push(...await validate(propSchema.getResolvedClassType(), propertyValue, propertyPath));
errors.push(...validate(propSchema.getResolvedClassType(), propertyValue, propertyPath));
}
}
}
Expand Down

0 comments on commit f9fe626

Please sign in to comment.