@travetto/schema
Advanced tools
Comparing version 3.1.0-rc.0 to 3.1.0-rc.1
{ | ||
"name": "@travetto/schema", | ||
"version": "3.1.0-rc.0", | ||
"version": "3.1.0-rc.1", | ||
"description": "Data type registry for runtime validation, reflection and binding.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -19,5 +19,6 @@ import { Class, ClassInstance } from '@travetto/base'; | ||
if (descOrIdx !== undefined && typeof descOrIdx === 'number') { | ||
property = `${property}.${descOrIdx}`; | ||
SchemaRegistry.registerPendingParamFacet(target.constructor, property!, descOrIdx, config); | ||
} else { | ||
SchemaRegistry.registerPendingFieldFacet(target.constructor, property!, config); | ||
} | ||
SchemaRegistry.registerPendingFieldFacet(target.constructor, property!, config); | ||
} else { | ||
@@ -24,0 +25,0 @@ SchemaRegistry.register(target, config); |
@@ -31,3 +31,2 @@ import { Class, AppError, ObjectUtil, ClassInstance, ConcreteClass } from '@travetto/base'; | ||
#pendingViews = new Map<Class, Map<string, ViewFieldsConfig<unknown>>>(); | ||
#methodSchemas = new Map<Class, Map<string, FieldConfig[]>>(); | ||
@@ -195,2 +194,3 @@ constructor() { | ||
metadata: {}, | ||
methods: {}, | ||
views: { | ||
@@ -230,18 +230,3 @@ [AllViewⲐ]: { | ||
getMethodSchema<T>(cls: Class<T>, method: string): FieldConfig[] { | ||
if (!this.#methodSchemas.has(cls)) { | ||
this.#methodSchemas.set(cls, new Map()); | ||
} | ||
const cache = this.#methodSchemas.get(cls)!; | ||
if (!cache.has(method) && this.has(cls)) { | ||
const { fields, schema } = this.getViewSchema(cls); | ||
const out = []; | ||
for (const el of fields) { | ||
if (el.startsWith(`${method}.`) && schema[el].forMethod) { | ||
out.push(schema[el]); | ||
} | ||
} | ||
out.sort((a, b) => a.index! - b.index!); | ||
cache.set(method, out); | ||
} | ||
return cache.get(method)! ?? []; | ||
return (this.get(cls)?.methods?.[method] ?? []).filter(x => !!x).sort((a, b) => a.index! - b.index!); | ||
} | ||
@@ -271,5 +256,14 @@ | ||
*/ | ||
registerPendingParamFacet(target: Class, prop: string, idx: number, config: Partial<FieldConfig>): Class { | ||
config.index = idx; | ||
return this.registerPendingFieldFacet(target, `${prop}.${idx}`, config); | ||
registerPendingParamFacet(target: Class, method: string, idx: number, config: Partial<FieldConfig>): Class { | ||
const methods = this.getOrCreatePending(target)!.methods!; | ||
const params = (methods[method] ??= []); | ||
params[idx] = { | ||
// @ts-expect-error | ||
name: `${method}.${idx}`, | ||
...params[idx] ?? {}, | ||
owner: target, | ||
index: idx, | ||
...config, | ||
}; | ||
return target; | ||
} | ||
@@ -307,6 +301,7 @@ | ||
registerPendingParamConfig(target: Class, method: string, idx: number, type: ClassList, conf?: Partial<FieldConfig>): Class { | ||
conf ??= {}; | ||
conf.index = idx; | ||
conf.forMethod = true; | ||
return this.registerPendingFieldConfig(target, `${method}.${idx}`, type, conf); | ||
return this.registerPendingParamFacet(target, method, idx, { | ||
...conf, | ||
array: Array.isArray(type), | ||
type: Array.isArray(type) ? type[0] : type, | ||
}); | ||
} | ||
@@ -343,2 +338,3 @@ | ||
}; | ||
dest.methods = { ...src.methods ?? {}, ...dest.methods ?? {} }; | ||
dest.metadata = { ...src.metadata ?? {}, ...dest.metadata ?? {} }; | ||
@@ -420,3 +416,2 @@ dest.subType = src.subType || dest.subType; | ||
this.#typeKeys.delete(cls); | ||
this.#methodSchemas.delete(cls); | ||
this.#accessorDescriptors.delete(cls); | ||
@@ -423,0 +418,0 @@ |
@@ -74,2 +74,6 @@ import { Primitive, Class } from '@travetto/base'; | ||
metadata?: Record<symbol, unknown>; | ||
/** | ||
* Method parameter configs | ||
*/ | ||
methods: Record<string, FieldConfig[]>; | ||
} | ||
@@ -164,8 +168,4 @@ | ||
accessor?: string; | ||
/** | ||
* Is the field for a method | ||
*/ | ||
forMethod?: boolean; | ||
} | ||
export type ViewFieldsConfig<T> = { with: Extract<(keyof T), string>[] } | { without: Extract<(keyof T), string>[] }; |
@@ -43,3 +43,3 @@ import { Class, ClassInstance, TypedObject, ObjectUtil } from '@travetto/base'; | ||
for (const field of fields) { | ||
if (schema[field].access !== 'readonly' && !schema[field].forMethod) { // Do not validate readonly fields | ||
if (schema[field].access !== 'readonly') { // Do not validate readonly fields | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
@@ -111,3 +111,4 @@ errors = errors.concat(this.#validateFieldSchema(schema[field], o[field as keyof T], relative)); | ||
const f = field[key]!; | ||
if (typeof f.n === 'number') { | ||
const fn = f.n; | ||
if (typeof fn === 'number') { | ||
if (typeof value === 'string') { | ||
@@ -119,9 +120,12 @@ value = parseInt(value, 10); | ||
} | ||
if (key === 'min' && value < f.n || key === 'max' && value > f.n) { | ||
const valN = typeof value === 'number' ? value : value.getTime(); | ||
if (key === 'min' && valN < fn || key === 'max' && valN > fn) { | ||
return true; | ||
} | ||
} else { | ||
const date = f.n.getTime(); | ||
const date = fn.getTime(); | ||
if (typeof value === 'string') { | ||
value = Date.parse(value); | ||
} else if (value instanceof Date) { | ||
value = value.getTime(); | ||
} | ||
@@ -128,0 +132,0 @@ if (key === 'min' && value < date || key === 'max' && value > date) { |
87665