-
Notifications
You must be signed in to change notification settings - Fork 23
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
Help converting a complex json object #60
Comments
Hello @appsolutegeek, thank for using You can achieve what you want using 2 similar approaches:
Considering the source is: const source = {
"_embedded": {
"record": {
"name": "CompanyA",
"meta": {
"address": "6 some street",
"stateCountry": "NY, USA"
}
}
},
"pageData": {
"pagination": {
"page": 1,
"totalPages": 1
}
}
} With 1 single complex schemaconst complexSchema = createSchema<Item, typeof source>({
name: '_embedded.record.name',
address: '_embedded.record.meta.address',
state: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[0],
country: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[1],
pagination: {
page: 'pageData.pagination.page',
totalPages: 'pageData.pagination.totalPages',
hasNext: ({ pageData: { pagination } }) => !(pagination.page === pagination.totalPages)
}
})
morphism(complexSchema, source) With 2 nested schemasconst paginationSchema: StrictSchema<Pagination, typeof source> = {
page: 'pageData.pagination.page',
totalPages: 'pageData.pagination.totalPages',
hasNext: ({ pageData: { pagination } }) => !(pagination.page === pagination.totalPages)
}
const nestedSchema = createSchema<Item, typeof source>({
name: '_embedded.record.name',
address: '_embedded.record.meta.address',
state: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[0],
country: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[1],
pagination: morphism(paginationSchema) // Map the Pagination object using the schema above
})
morphism(nestedSchema, source) Here's a working playground if you want to test it: https://stackblitz.com/edit/morphism-single-source-to-complex-destination Is it what you were looking for ? Please let me know if there are some parts where you need further explanation. |
That was perfect! Thank you! |
I am trying to type this but it's not an easy one, the only way I found to
support this would be to have an array of string representing the property
path see microsoft/TypeScript#12290. If you want
to make the most of strictly typing you could use a function instead `name:
(source) => source._embedded.record.name`, destructuring the object works
as well :)
…On Tue, 9 Apr 2019 at 06:13, Ian Gregson ***@***.***> wrote:
Just one question, this line
name: '_embedded.record.name',
Is it possible to keep it strictyly typed, i mean instead of passing in a
string as its prone to error.
i.e.
_embedded.record.name
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#60 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEh0g1VqnPv4yacuXM8NvkAdeLssg0Jvks5vfGfdgaJpZM4ciV4E>
.
--
Yann RENAUDIN <http://linkedin.com/in/renaudinyann> | Développeur et
Architecte de solutions | * renaudin.yann@gmail.com | +33629826437
|
Sorry @emyann , I think that was a comment I left previously, I actually deleted it because sure enough, it was as easy as that. Although it got it on iteretee
|
Perfect, thank you! Closing this one :) |
Hi,
I have a json object, its not complex but what I want to end up, is a little difficult for me to understand how to do this. The standard JSON object (represented by a typescript interface) is this
I want to end up with the following, represented by a typescript class
Any help really appreciated
Thanks
The text was updated successfully, but these errors were encountered: