@travetto/schema
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "0.0.16" | ||
"version": "0.0.17" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { CommonRegExp, SchemaRegistry, ClassList } from '../service'; | ||
import { CommonRegExp, SchemaRegistry, ClassList, ValidatorFn } from '../service'; | ||
@@ -3,0 +3,0 @@ function prop(obj: { [key: string]: any }) { |
import { Field } from './field'; | ||
import { SchemaRegistry } from '../service'; | ||
import { SchemaRegistry, ValidatorFn } from '../service'; | ||
import { Class } from '@travetto/registry'; | ||
export function Schema(auto: boolean = true): ClassDecorator { | ||
return (target: any) => { | ||
SchemaRegistry.register(target, {}); | ||
return (target: Class<any>) => { | ||
SchemaRegistry.getOrCreatePending(target); | ||
}; | ||
} | ||
} | ||
export function Validator<T>(fn: ValidatorFn<T, string>) { | ||
return (target: Class<T>) => { | ||
SchemaRegistry.getOrCreatePending(target).validators!.push(fn); | ||
}; | ||
}; |
import { ClassList, FieldConfig, ClassConfig, ViewConfig } from './types'; | ||
import { MetadataRegistry, RootRegistry, Class } from '@travetto/registry'; | ||
export class $SchemaRegistry extends MetadataRegistry<ClassConfig> { | ||
export class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> { | ||
@@ -16,2 +16,3 @@ static DEFAULT_VIEW = '__all'; | ||
class: cls, | ||
validators: [], | ||
views: { | ||
@@ -118,2 +119,3 @@ [$SchemaRegistry.DEFAULT_VIEW]: { | ||
} | ||
dest.validators = [...src.validators, ...dest.validators]; | ||
return dest; | ||
@@ -120,0 +122,0 @@ } |
import { Class } from '@travetto/registry'; | ||
import { ValidationError } from '.'; | ||
@@ -9,2 +10,4 @@ export interface SchemaClass<T = any> { | ||
export type ValidatorFn<T, U> = (value: T, parent?: U) => ValidationError | undefined; | ||
export interface SchemaConfig { | ||
@@ -21,2 +24,3 @@ [key: string]: FieldConfig; | ||
views: { [key: string]: ViewConfig }; | ||
validators: ValidatorFn<any, any>[]; | ||
} | ||
@@ -23,0 +27,0 @@ |
@@ -17,3 +17,3 @@ import { FieldConfig, SchemaConfig } from '../types'; | ||
static validateField(field: FieldConfig, value: any) { | ||
static validateField(field: FieldConfig, value: any, parent: any) { | ||
const criteria: string[] = []; | ||
@@ -90,2 +90,3 @@ | ||
} | ||
return errors; | ||
@@ -145,3 +146,3 @@ } | ||
for (let i = 0; i < val.length; i++) { | ||
const subErrors = this.validateField(fieldSchema, val[i]); | ||
const subErrors = this.validateField(fieldSchema, val[i], o); | ||
errors.push(...this.prepareErrors(`${path}[${i}]`, subErrors)); | ||
@@ -154,3 +155,3 @@ } | ||
} else { | ||
const fieldErrors = this.validateField(fieldSchema, val); | ||
const fieldErrors = this.validateField(fieldSchema, val, o); | ||
errors.push(...this.prepareErrors(path, fieldErrors)); | ||
@@ -166,5 +167,13 @@ } | ||
const config = SchemaRegistry.getViewSchema(cls, view); | ||
const validators = SchemaRegistry.get(cls).validators; | ||
const errors = this.validateSchema(config.schema, o, view, ''); | ||
for (const fn of validators) { | ||
const res = fn(o, view); | ||
if (res) { | ||
errors.push(res); | ||
} | ||
} | ||
if (errors.length) { | ||
@@ -171,0 +180,0 @@ throw new ValidationErrors(errors); |
@@ -1,2 +0,2 @@ | ||
import { Field, MinLength, Url, SchemaBound, Required, SchemaValidator, Enum, Schema, ValidationError, SchemaRegistry, ValidationErrors } from '../src'; | ||
import { Field, MinLength, Url, SchemaBound, Required, SchemaValidator, Enum, Schema, ValidationError, SchemaRegistry, ValidationErrors, Validator } from '../src'; | ||
import { Suite, Test, BeforeAll, ShouldThrow } from '@travetto/test'; | ||
@@ -43,2 +43,19 @@ import * as assert from 'assert'; | ||
@Schema() | ||
@Validator((o: any) => { | ||
if ((o.age + o.age2) % 2 === 0) { | ||
return { | ||
kind: 'custom', | ||
message: 'age1 + age2 cannot be even', | ||
path: 'age1' | ||
}; | ||
} | ||
}) | ||
class CustomValidated extends SchemaBound { | ||
age: number; | ||
age2: number; | ||
} | ||
function findError(errors: ValidationError[], path: string, message: string) { | ||
@@ -126,2 +143,19 @@ return errors.find(x => x.path === path && x.message.includes(message)); | ||
} | ||
} | ||
@Test('Custom Validators') | ||
async validateFields() { | ||
const obj = CustomValidated.from({ | ||
age: 200, | ||
age2: 10 | ||
}); | ||
try { | ||
await SchemaValidator.validate(obj); | ||
assert(false); | ||
} catch (e) { | ||
assert((e as ValidationErrors).errors[0].path === 'age1'); | ||
assert((e as ValidationErrors).errors[0].kind === 'custom'); | ||
assert((e as ValidationErrors).errors[0].message === 'age1 + age2 cannot be even'); | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40815
1167