Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PropSchemas inside of custom PropSchema functions #176

Open
earlAchromatic opened this issue Nov 30, 2022 · 1 comment
Open

Use PropSchemas inside of custom PropSchema functions #176

earlAchromatic opened this issue Nov 30, 2022 · 1 comment

Comments

@earlAchromatic
Copy link

earlAchromatic commented Nov 30, 2022

I am trying to serialize a Set (using custom propSchema like in #157), but would like the result to use a reference inside the custom PropSchema to point to objects in the Set.

This doesn't work though:

import {identifier, reference, createModelSchema} from 'serializr'
// transmitters is a Set
// Transmitter is the class of each Set item

createModelSchema(Receiver, {
  id: identifier(),
// Want this to be a list of refs
  transmitters: custom((model) => {
    const array = Array.from(model)
    return array.map(() => {
      return reference(Transmitter)
    })
  }, deserializer)
})

Is there a way to do this?

@NaridaL
Copy link
Collaborator

NaridaL commented Dec 11, 2022

You can't use reference in that manner as it returns a PropSchema, not an actual object.

You should be able to achieve what you are trying with the following. The docs could be improved though.

createModelSchema(Receiver, {
  id: identifier(),
// Want this to be a list of refs
  transmitters: custom((model) => {
    const array = Array.from(model)
    return array.map(x => x.id /* or whatever is the id prop for transmitters */)
  }, 
 (identifierValue, done, context) => context.rootContext.await(Transmitter, identifierValue, done);)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants