@travetto/schema
Advanced tools
Comparing version 3.4.4 to 3.4.5
{ | ||
"name": "@travetto/schema", | ||
"version": "3.4.4", | ||
"description": "Data type registry for runtime validation, reflection and binding.", | ||
"version": "3.4.5", | ||
"description": "Data type registry for runtime validation, reflection, and binding.", | ||
"keywords": [ | ||
@@ -30,6 +30,6 @@ "schema", | ||
"dependencies": { | ||
"@travetto/registry": "^3.4.2" | ||
"@travetto/registry": "^3.4.1" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/transformer": "^3.4.2" | ||
"@travetto/transformer": "^3.4.3" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependenciesMeta": { |
@@ -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
93027
2086
Updated@travetto/registry@^3.4.1