question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing attributes during conversion using an ActionFunction with morphism()

See original GitHub issue

If I have a definition such as:

interface Grade = {
  level: 'A'|'B'|'C'
  note?: string
}
interface Student = {
  name: string
  grade?: Grade
}

interface StudentDocument = {
  name: string,
  grade_data?: {
    level: 'A'|'B'|'C'
    note?: string
  }
}

const gradeSchema: StrictSchema<Grade> = {
  level: 'level',
  note: 'note'
}
const studentSchema: StrictSchema<Student, StudentDocument> = {
  name: 'name',
  grade: ({ grade_data }: any) => morphism(gradeSchema, grade_data),
}

Then the following function call will give:

morphism(studentSchema, { name: 'John Doe' })
{ name: 'John Doe',
  grade: [Function] }

I can wrap this in something like:

grade: ({ grade_data }: any) => grade_data ? morphism(gradeSchema, grade_data) : null

However it would be nice if the morphism never resulted in a function even with missing values. What is the strategy when working with optional values?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

10reactions
emyanncommented, Mar 23, 2019

@terlar Thank you for your feedback !I did release a new version 1.10 with which it is possible to either strip the key when the value is undefined or provide a default value to replace undefined ones.

To do so it is now possible to create a schema with options using the createSchema function.

Strip undefined value

import { createSchema, morphism } from 'morphism'

const schema = createSchema<Target>({ keyA: 'somepath' }, { undefinedValues: { strip: true } })
// => {}

Default value fallback

import { createSchema, morphism } from 'morphism'

const schema = createSchema(
  { keyA: 'somepath' },
  {
    undefinedValues: {
      strip: true,
      default: () => null
    }
  }
);
// => { keyA: null }

Here’s a playground if you want to see it working: https://repl.it/@yrnd1/Morphism-undefined-values

Please let me know what you think about it and don’t hesitate if you have questions 😃

2reactions
terlarcommented, May 23, 2019

Sorry for the delay, I didn’t have time to update my implementation to use this until now. Just want to return here to say thank you and I am very satisfied with the current behavior. Keep up the good work 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to pass parameter using Action Function
I'm missing the name attribute while using the apex:param tag.So that i s causing the problem. Although i'm not sure why we require...
Read more >
actionFunction working only when I have certain parameters ...
I am having a weird issue when using apex:actionFunction as it only works sometimes (Only when I am passing a parameter in the...
Read more >
Part III Profinite Groups - DPMMS
One group that obviously satisfies the properties of Z in the above diagram is G itself. So there is a natural homomorphism ι:...
Read more >
Notes on Computational Group heory
to construct these groups and determine their properties. ... a differential equation, in the same way as she would use Maple to solve...
Read more >
F# for Fun and Profit
Other interesting ways of using F# at work ... mindset, and not realize what you are missing. ... let nextLineFn = fun() ->...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found