@travetto/schema
Advanced tools
Comparing version 4.0.0-rc.6 to 4.0.0-rc.7
{ | ||
"name": "@travetto/schema", | ||
"version": "4.0.0-rc.6", | ||
"version": "4.0.0-rc.7", | ||
"description": "Data type registry for runtime validation, reflection and binding.", | ||
@@ -30,6 +30,6 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/registry": "^4.0.0-rc.6" | ||
"@travetto/registry": "^4.0.0-rc.7" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/transformer": "^4.0.0-rc.5" | ||
"@travetto/transformer": "^4.0.0-rc.6" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependenciesMeta": { |
@@ -223,16 +223,18 @@ <!-- This file was generated by @travetto/doc and should not be modified directly --> | ||
"at": "2029-03-14T04:00:00.618Z", | ||
"errors": [ | ||
{ | ||
"kind": "type", | ||
"type": "number", | ||
"message": "age is not a valid number", | ||
"path": "age" | ||
}, | ||
{ | ||
"kind": "required", | ||
"active": true, | ||
"message": "address.street2 is required", | ||
"path": "address.street2" | ||
} | ||
] | ||
"details": { | ||
"errors": [ | ||
{ | ||
"kind": "type", | ||
"type": "number", | ||
"message": "age is not a valid number", | ||
"path": "age" | ||
}, | ||
{ | ||
"kind": "required", | ||
"active": true, | ||
"message": "address.street2 is required", | ||
"path": "address.street2" | ||
} | ||
] | ||
} | ||
} | ||
@@ -367,10 +369,12 @@ ``` | ||
"at": "2029-03-14T04:00:00.837Z", | ||
"errors": [ | ||
{ | ||
"kind": "type", | ||
"type": "PointImpl", | ||
"message": "point is not a valid PointImpl", | ||
"path": "point" | ||
} | ||
] | ||
"details": { | ||
"errors": [ | ||
{ | ||
"kind": "type", | ||
"type": "PointImpl", | ||
"message": "point is not a valid PointImpl", | ||
"path": "point" | ||
} | ||
] | ||
} | ||
} | ||
@@ -377,0 +381,0 @@ ``` |
@@ -59,12 +59,3 @@ import { Class, ClassInstance, TypedObject, ObjectUtil } from '@travetto/base'; | ||
static #validateFieldSchema(fieldSchema: FieldConfig, val: unknown, relative: string = ''): ValidationError[] { | ||
return this.#validateFieldSchemaRaw(fieldSchema, val, `${relative}${relative ? '.' : ''}${fieldSchema.name}`); | ||
} | ||
/** | ||
* Validate a single field config against a passed in value | ||
* @param fieldSchema The field schema configuration | ||
* @param val The raw value, could be an array or not | ||
* @param path The current path of validation traversal | ||
*/ | ||
static #validateFieldSchemaRaw(fieldSchema: FieldConfig, val: unknown, path: string = ''): ValidationError[] { | ||
const path = `${relative}${relative ? '.' : ''}${fieldSchema.name}`; | ||
const hasValue = !(val === undefined || val === null || (typeof val === 'string' && val === '') || (Array.isArray(val) && val.length === 0)); | ||
@@ -246,23 +237,13 @@ | ||
/** | ||
* Validate an object against it's constructor's schema | ||
* @param cls The class to validate the objects against | ||
* @param o The object to validate | ||
* @param view The optional view to limit the scope to | ||
* Validate the class level validations | ||
*/ | ||
static async validate<T>(cls: Class<T>, o: T, view?: string): Promise<T> { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
if (!ObjectUtil.isPlainObject(o) && !(o instanceof cls || cls.Ⲑid === (o as ClassInstance<T>).constructor.Ⲑid)) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
throw new TypeMismatchError(cls.name, (o as ClassInstance).constructor.name); | ||
static async #validateClassLevel<T>(cls: Class<T>, o: T, view?: string): Promise<ValidationError[]> { | ||
const schema = SchemaRegistry.get(cls); | ||
if (!schema) { | ||
return []; | ||
} | ||
cls = SchemaRegistry.resolveSubTypeForInstance(cls, o); | ||
const config = SchemaRegistry.getViewSchema(cls, view); | ||
const validators = SchemaRegistry.get(cls).validators; | ||
// Validate using standard behaviors | ||
const errors = this.#validateSchema(config.schema, o, ''); | ||
const errors: ValidationError[] = []; | ||
// Handle class level validators | ||
for (const fn of validators) { | ||
for (const fn of schema.validators) { | ||
try { | ||
@@ -281,3 +262,26 @@ const res = await fn(o, view); | ||
} | ||
return errors; | ||
} | ||
/** | ||
* Validate an object against it's constructor's schema | ||
* @param cls The class to validate the objects against | ||
* @param o The object to validate | ||
* @param view The optional view to limit the scope to | ||
*/ | ||
static async validate<T>(cls: Class<T>, o: T, view?: string): Promise<T> { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
if (!ObjectUtil.isPlainObject(o) && !(o instanceof cls || cls.Ⲑid === (o as ClassInstance<T>).constructor.Ⲑid)) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
throw new TypeMismatchError(cls.name, (o as ClassInstance).constructor.name); | ||
} | ||
cls = SchemaRegistry.resolveSubTypeForInstance(cls, o); | ||
const config = SchemaRegistry.getViewSchema(cls, view); | ||
// Validate using standard behaviors | ||
const errors = [ | ||
...this.#validateSchema(config.schema, o, ''), | ||
... await this.#validateClassLevel(cls, o, view) | ||
]; | ||
if (errors.length) { | ||
@@ -333,3 +337,10 @@ throw new ValidationResultError(errors); | ||
for (const field of SchemaRegistry.getMethodSchema(cls, method)) { | ||
errors.push(...this.#validateFieldSchemaRaw(field, params[field.index!], prefixes[field.index!])); | ||
const i = field.index!; | ||
errors.push(...[ | ||
... this.#validateFieldSchema(field, params[i]), | ||
... await this.#validateClassLevel(field.type, params[i]) | ||
].map(x => { | ||
x.path = !prefixes[i] ? x.path.replace(`${field.name}.`, '') : x.path.replace(field.name, prefixes[i]!); | ||
return x; | ||
})); | ||
} | ||
@@ -336,0 +347,0 @@ if (errors.length) { |
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
101982
2289
391