Skip to content

Lack of usage on validation #1765

Answered by jamonholmgren
geohuz asked this question in Q&A
Discussion options

You must be logged in to vote

We should add some examples to the documentation, for sure.

Here's one example that might help:

https://mobx-state-tree.js.org/API/#custom

const DecimalPrimitive = types.custom<string, Decimal>({
    name: "Decimal",
    fromSnapshot(value: string) {
        return new Decimal(value)
    },
    toSnapshot(value: Decimal) {
        return value.toString()
    },
    isTargetType(value: string | Decimal): boolean {
        return value instanceof Decimal
    },
    getValidationMessage(value: string): string {
        if (/^-?\d+\.\d+$/.test(value)) return "" // OK
        return `'${value}' doesn't look like a valid decimal number`
    }
})

const Wallet = types.model({
    balance: Decima…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jamonholmgren
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1763 on August 17, 2021 16:35.