@travetto/schema
Advanced tools
Comparing version 3.1.8 to 3.1.9
{ | ||
"name": "@travetto/schema", | ||
"version": "3.1.8", | ||
"version": "3.1.9", | ||
"description": "Data type registry for runtime validation, reflection and binding.", | ||
@@ -30,3 +30,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/registry": "^3.1.4" | ||
"@travetto/registry": "^3.1.5" | ||
}, | ||
@@ -33,0 +33,0 @@ "peerDependencies": { |
@@ -58,6 +58,6 @@ import { Class, ConcreteClass, TypedObject, ObjectUtil, DataUtil } from '@travetto/base'; | ||
const part = parts.shift()!; | ||
const arr = part.indexOf('[') > 0; | ||
const partArr = part.indexOf('[') > 0; | ||
const name = part.split(/[^A-Za-z_0-9]/)[0]; | ||
const idx = arr ? part.split(/[\[\]]/)[1] : ''; | ||
const key = arr ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined; | ||
const idx = partArr ? part.split(/[\[\]]/)[1] : ''; | ||
const key = partArr ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined; | ||
@@ -77,3 +77,5 @@ if (!(name in sub)) { | ||
if (last.indexOf('[') < 0) { | ||
const arr = last.indexOf('[') > 0; | ||
if (!arr) { | ||
if (sub[last] && ObjectUtil.isPlainObject(val)) { | ||
@@ -85,21 +87,19 @@ sub[last] = DataUtil.deepAssign(sub[last], val, 'coerce'); | ||
} else { | ||
const arr = last.indexOf('[') > 0; | ||
const name = last.split(/[^A-Za-z_0-9]/)[0]; | ||
const idx = arr ? last.split(/[\[\]]/)[1] : ''; | ||
const idx = last.split(/[\[\]]/)[1]; | ||
let key = arr ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined; | ||
if (sub[name] === undefined) { | ||
sub[name] = (typeof key === 'string') ? {} : []; | ||
let key = (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)); | ||
sub[name] ??= (typeof key === 'string') ? {} : []; | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const arrSub = sub[name] as Record<string, unknown>; | ||
if (key === undefined) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
sub = sub[name] as Record<string, unknown>; | ||
if (key === undefined) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
key = sub.length as number; | ||
} | ||
if (sub[key] && ObjectUtil.isPlainObject(val) && ObjectUtil.isPlainObject(sub[key])) { | ||
sub[key] = DataUtil.deepAssign(sub[key], val, 'coerce'); | ||
} else { | ||
sub[key] = val; | ||
} | ||
key = arrSub.length as number; | ||
} | ||
if (arrSub[key] && ObjectUtil.isPlainObject(val) && ObjectUtil.isPlainObject(arrSub[key])) { | ||
arrSub[key] = DataUtil.deepAssign(arrSub[key], val, 'coerce'); | ||
} else { | ||
arrSub[key] = val; | ||
} | ||
} | ||
@@ -115,4 +115,4 @@ } | ||
*/ | ||
static flattenPaths(data: Record<string, unknown>, prefix: string = ''): Record<string, unknown> { | ||
const out: Record<string, unknown> = {}; | ||
static flattenPaths<V extends string = string>(data: Record<string, unknown>, prefix: string = ''): Record<string, V> { | ||
const out: Record<string, V> = {}; | ||
for (const [key, value] of Object.entries(data)) { | ||
@@ -133,3 +133,4 @@ const pre = `${prefix}${key}`; | ||
} else { | ||
out[pre] = value ?? ''; | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
out[pre] = (value ?? '') as V; | ||
} | ||
@@ -281,2 +282,5 @@ } | ||
} | ||
if (!field.required && (val === undefined || val === null)) { | ||
return val; | ||
} | ||
const complex = SchemaRegistry.has(field.type); | ||
@@ -283,0 +287,0 @@ if (field.array) { |
@@ -114,3 +114,4 @@ import ts from 'typescript'; | ||
const values = typeExpr.subTypes.map(x => x.key === 'literal' ? x.value : undefined) | ||
.filter(x => x !== undefined && x !== null); | ||
.filter(x => x !== undefined && x !== null) | ||
.sort(); | ||
@@ -214,7 +215,7 @@ if (values.length === typeExpr.subTypes.length) { | ||
case 'managed': out.type = state.typeToIdentifier(type); break; | ||
case 'shape': out.type = SchemaTransformUtil.toConcreteType(state, type, target); break; | ||
case 'shape': out.type = this.toConcreteType(state, type, target); break; | ||
case 'literal': { | ||
if (type.ctor) { | ||
out.type = out.array ? | ||
SchemaTransformUtil.toConcreteType(state, type, target) : | ||
this.toConcreteType(state, type, target) : | ||
state.factory.createIdentifier(type.ctor.name); | ||
@@ -221,0 +222,0 @@ } |
90184
2019
Updated@travetto/registry@^3.1.5