Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,41 +152,55 @@ function getSchemaForPath(path: string, schema: ZodSchema): ZodSchema | null {
if (!isObjectSchema(schema)) {
return null;
}

if (isNotNestedPath(path)) {
return schema.shape[cleanupNonNestedPath(path)];
}

const paths = (path || '').split(/\.|\[(\d+)\]/).filter(Boolean);

let currentSchema: ZodSchema = schema;
for (let i = 0; i <= paths.length; i++) {

for (let i = 0; i < paths.length; i++) {
const p = paths[i];
if (!p || !currentSchema) {
return currentSchema;
}


if (isArraySchema(currentSchema) && isIndex(p)) {
currentSchema = (currentSchema._def as ZodArrayDef).element;
continue;
}

if (isObjectSchema(currentSchema)) {
currentSchema = currentSchema.shape[p] || null;
continue;
}

if (isIndex(p) && isArraySchema(currentSchema)) {
currentSchema = (currentSchema._def as ZodArrayDef).type;
}

break;
}

return null;
return currentSchema;
}

function getDefType(schema: ZodSchema) {
return (schema._def as any).typeName as ZodFirstPartyTypeKind;
function getDefType(schema: ZodSchema): ZodFirstPartyTypeKind | undefined {
if (schema._def && (schema._def as any).typeName) {
return (schema._def as any).typeName as ZodFirstPartyTypeKind;
}

if (schema._def && (schema._def as any).type) {
if ((schema._def as any).type === 'array') return ZodFirstPartyTypeKind.ZodArray;
if ((schema._def as any).type === 'object') return ZodFirstPartyTypeKind.ZodObject;
}

return undefined;
}

function isArraySchema(schema: ZodSchema): schema is ZodArray<any, any> {
return getDefType(schema) === ZodFirstPartyTypeKind.ZodArray;
if (!schema || !schema._def) return false;
return (schema._def as any).type === 'array' || getDefType(schema) === ZodFirstPartyTypeKind.ZodArray;
}

function isObjectSchema(schema: ZodSchema): schema is AnyZodObject {
return getDefType(schema) === ZodFirstPartyTypeKind.ZodObject;
if (!schema || !schema._def) return false;
return (schema._def as any).type === 'object' || getDefType(schema) === ZodFirstPartyTypeKind.ZodObject;
}