Skip to content

Sanitizing misidentifies diamond-shaped references as cycles #367

Description

@SkelatorIndy

This is a very similar issue to #348, but occurs when the same record is referenced from two different paths in a create call.

For example, this code results in the same type of validation error as #348:

import{Collection}from'@msw/data';import*asvfrom'valibot';constprofileSchema=v.object({id: v.string(),bio: v.string(),});constuserSchema=v.object({id: v.string(),name: v.string(),getprofile(){returnprofileSchema;},});constpostSchema=v.object({id: v.string(),title: v.string(),getauthor(){returnuserSchema;},geteditor(){returnuserSchema;},});constprofiles=newCollection({schema: profileSchema});constusers=newCollection({schema: userSchema});constposts=newCollection({schema: postSchema});users.defineRelations({ one })=>({profile: one(profiles)}));posts.defineRelations({ one })=>({author: one(users),editor: one(users),}));constprofile=awaitprofiles.create({id: 'p1',bio: 'hi'});constuser=awaitusers.create({id: 'u1',name: 'Alice', profile });// Diamond: same user referenced from two sibling fieldsawaitposts.create({id: 'post1',title: 'Hello',author: user,editor: user,});

In this setup, either author.profile or editor.profile will be undefined after sanitization.

From my testing, I've been able to fix the error by adding the following to the inner sanitize method within the #sanitizeInitialValues method:

constresult=Object.fromEntries(...);if(record&&!isRevisit){visited.delete(record[kPrimaryKey]);}returnresult;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions