lolopinto / ent

MIT License
52 stars 6 forks source link

`transformWrite` fails when `StructType` fields are included in data set in the return object #1765

Closed Swahvay closed 9 months ago

Swahvay commented 9 months ago

When using transformWrite and setting data in the data key in the return object, if any of those fields are StructType fields, then it will transform the object keys of that StructType object to getStorageKey values, but then later when validating the values for edit here: https://github.com/lolopinto/ent/blob/0b14f75ca8f10bca63ef42cfc6ed99bf39566d7f/ts/src/action/orchestrator.ts#L1165 it throws an error because it's expecting the un-altered keys.

Swahvay commented 9 months ago

It happens here: https://github.com/lolopinto/ent/blob/0b14f75ca8f10bca63ef42cfc6ed99bf39566d7f/ts/src/action/orchestrator.ts#L1014

The struct field value is actually getting JSON stingified

Swahvay commented 9 months ago

Commenting out that format line makes everything work as expected. There is another format call later on in transformFieldValue just after where the error above is being thrown. So maybe that's all the fix that's needed?

lolopinto commented 9 months ago

I can't repro this in my tests in both v0.1.x and v0.2.0

can you give more context? what type of fields? what type of transform?

https://github.com/lolopinto/ent/compare/fix-1765?expand=1

Swahvay commented 9 months ago

I'm doing a transformWrite on a create action to transform it into an edit, and passing a new set of data in the returned object. Basically I'm doing a deep-merge on the existing ent and the new data passed into it. One of the fields is defined as:

horses: StructTypeAsList({
  tsType: 'CompetitionEventHorseEntry',
  fields: {
    horseId: UUIDType({
      nullable: true,
      foreignKey: {
        schema: 'Horse',
        column: 'ID',
      },
    }),
    horseName: StringType({
      nullable: true,
      maxLen: 200,
    }),
  },
}),

And when going through the transform, it re-writes it into a JSON string with the DB storage keys instead of the camelCase keys. That then fails the validation later on.

lolopinto commented 9 months ago

ah, i misunderstood, you're transforming the result. will take a look later at that and try and fix it

lolopinto commented 9 months ago

ok, i finally repro. I was returning a Map instead of {} and so I wasn't running into this :(

lolopinto commented 9 months ago

closing this so I can keep track. will be in new versions soon