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.

add support for flat name for dot notation

See original GitHub issue

Is your feature request related to a problem? Please describe.

To avoid mapping between form and api requests, sometimes I want my key as a flat string with dot notation, and not to be automatically converted to nested objects.

Describe the solution you’d like

To trigger or avoid automatically parsing or nesting dot notation in name, use some way to tell parser to ignore nesting the dot notation, perhaps wrap with quotes:

name="\"object.nested.db.filter.status.value\""
error={errors["\"object.nested.db.filter.status.value\""]}
helperText={errors["\"object.nested.db.filter.status.value\""]?.message}

EDIT: Use escaped double quotes to show how it would work better.

The dot notation could ignore automatically nesting because it contains or starts and ends with quotes ' or ".

Describe alternatives you’ve considered

I see that getValues will return the flat key, but it doesn’t feel right, I’d like to use normal submit handler if possible, but maybe I’m not seeing the right way to use it in this case.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ilovettcommented, May 8, 2020

Thanks @kotarella1110 . The types do indeed pass, however the parsed values sent to onSubmit is parsed with the ' prepended on first key, and appended to last key – returns values like

{ "\"object": { "nested": { "db": { "filter": { "status": { "value\"": "good" }}}}}}

It would be cool if instead of nesting, first checks if it’s a string if it contains ' (or some other way of telling RHF not to automatically transformToNestObject) and returns flat:

{ "object.nested.db.filter.status.value": "good" }

Not sure if thats very intuitive or maybe too hacky even. I am just going to close this ticket.

2reactions
kotarella1110commented, May 8, 2020

@ilovett Thanks for your suggestion. Because TS cannot parse the string literal type, this suggestion can cause to the following type error:

// This works
const { getValues, errors } = useForm<{
  "'object.nested.db.filter.status.value'": string;
}>();

name="'object.nested.db.filter.status.value'"
errors["'object.nested.db.filter.status.value'"]
getValues("'object.nested.db.filter.status.value'")

// This doesn't work
const { getValues, errors } = useForm<{
  "'object.nested.db.filter.status.value'": string;
}>();

name='"object.nested.db.filter.status.value"'
errors['"object.nested.db.filter.status.value"'] // It works in JS, but it's a type error in TS
getValues('"object.nested.db.filter.status.value"') // It works in JS, but it's a type error in TS
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can this flat (dot notation) to hierarchical object conversion be ...
I am trying to convert a flat object in dot notation (like MongoDb uses for the updates) into a hierarchical one.
Read more >
dot-and-bracket-notation-in-javascript - <capscode/>
In JavaScript we can access the properties of object using dot notation(person.first_name) and bracket notation(person[first_name]).
Read more >
Add support for Encoding and Decoding nested objects with ...
I propose that we add a new CodingKeyPath type that allows consumers to key into nested objects using dot notation.
Read more >
Escape table names in dot notation - Cloudera Documentation
The database name and the table name must be enclosed in backticks when using dot notation.
Read more >
Property accessors - JavaScript - MDN Web Docs - Mozilla
There are two ways to access properties: dot notation and bracket notation. Dot notation. In the object.propertyName syntax, the propertyName ...
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