0

I'm having trouble on working out a nice way to validate this structure

I've seen suggestions on flattening data like this, but it seems like this is the best way to store this information, as it allows instant access via a item ID / a combo of item ID + store ID.

The data looks like this

let data = {
    "itemid1" : {
        "store1" : {
            stockNow: 2,
            stockLater : 15,
            stockDate : "4 days"
        },
        "store2" : {
            stockNow : 2,
            stockLater : 10,
            stockDate : "4 days"
        },
        "store3" : {
            stockNow : 4,
            stockLater : 15,
            stockDate : "4 days"
        },
    },
    "itemid2" : {
        "store1" : {
            stockNow: 2,
            stockLater : 15,
            stockDate : "4 days"
        },
        "store2" : {
            stockNow : 2,
            stockLater : 10,
            stockDate : "4 days"
        },
        "store3" : {
            stockNow : 4,
            stockLater : 15,
            stockDate : "4 days"
        },
    }
}
1
  • You might consider reducing the duplication by having two props: stores is a list of the store objects and data has a list of store ids for each item id. Commented Mar 13, 2018 at 19:50

1 Answer 1

3

You can just do this:

EDIT: Didn't notice the itemID part.

MyComponent.propTypes = {
    data: PropTypes.objectOf(
        PropTypes.objectOf(
            PropTypes.shape({
                stockNow: PropTypes.number,
                stockLater: PropTypes.number,
                stockDate: PropTypes.string,
            })
        )
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I actually had it then, Makes sense now I guess after I read it twice :), thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.