icebob / fastest-validator

:zap: The fastest JS validator library for NodeJS
MIT License
1.42k stars 88 forks source link

TypeError: str.replace is not a function. #344

Closed TimiBolu closed 5 days ago

TimiBolu commented 3 months ago

Hi, I really appreciate your library and have used it ever since I found out about it (roughly a year).

However, I encountered an issue. When I tried to compile the following code, I get the error below,

* The code **

Screenshot 2024-05-24 at 10 51 06

The error

Screenshot 2024-05-24 at 10 45 56

When I change any two things, it works fine. 1 - If I remove the "form" field. or 2 - or I change the string types

Screenshot 2024-05-24 at 10 47 47

Please what could be the issue.

FerX commented 2 months ago

Please do not send screenshots, but the schema and example data.

TimiBolu commented 2 months ago
import Validator from "fastest-validator";

const v = new Validator();

const header = {
    "socials": {
        "$$type": "object|optional",
        "apps": {
            "type": "array",
            "items": {
                "type": "object",
                "props": {
                    "name": {
                        "type": "string",
                        "enum": [
                            "instagram",
                            "twitter",
                            "facebook",
                            "github",
                            "linkedin",
                            "tiktok",
                            "dribbble",
                            "behance",
                            "twitch",
                            "snapchat",
                            "email",
                            "whatsapp",
                            "webaddress"
                        ]
                    },
                    "handle": {
                        "type": "string",
                        "optional": true
                    },
                    "url": {
                        "type": "string",
                        "optional": true
                    }
                }
            },
            "optional": true
        },
        "iconStyle": {
            "type": "string",
            "optional": true,
            "enum": [
                "outline",
                "color",
                "solid"
            ]
        },
        "alignment": {
            "type": "string",
            "optional": true,
            "enum": [
                "left",
                "center",
                "right"
            ]
        }
    },
    "text": {
        "$$type": "object|optional",
        "value": {
            "type": "string",
            "optional": true
        },
        "font": {
            "type": "string",
            "optional": true
        }
    },
    "title": {
        "$$type": "object|optional",
        "heading": {
            "type": "string",
            "optional": true
        },
        "subHeading": {
            "type": "string",
            "optional": true
        },
        "alignment": {
            "type": "string",
            "optional": true,
            "enum": [
                "left",
                "center",
                "right"
            ],
            "default": "center"
        }
    },
    "image": {
        "$$type": "object",
        "value": {
            "type": "string",
            "optional": true
        },
        "style": {
            "type": "string",
            "optional": true,
            "enum": [
                "round",
                "straight",
                "curved",
                "curved-md"
            ]
        },
        "alignment": {
            "type": "string",
            "optional": true,
            "enum": [
                "full_width",
                "none"
            ]
        }
    },
    "layout": {
        "type": "string",
        "enum": [
            "img-right",
            "img-left",
            "img-top",
            "img-bg",
            "img-top-bg"
        ]
    },
    "form": {
        "$$type": "object|optional",
        "title": {
            "type": "string"
        },
        "description": {
            "type": "string"
        },
        "label": {
            "type": "string"
        },
        "field1": {
            "$$type": "object",
            "name": {
                "type": "string"
            },
            "value": {
                "type": "string"
            }
        },
        "field2": {
            "$$type": "object",
            "name": {
                "type": "string"
            },
            "value": {
                "type": "string"
            }
        }
    }
};

const ssc = v.compile(header);
console.log({ssc})

 const data = {
    "image": {
        "value": "https://res.cloudinary.com/dnsbjog0b/image/upload/v1705415231/fmm2oskwp6r2q8gu1ack.jpg",
        "style": "round",
        "alignment": "none"
    },
    "text": {
        "size": "narrow",
        "value": "With fingers flying over the keyboard, I turn code into symphonies, creating not just functional but stylish tech wonders."
    },
    "title": {
        "heading": "Timi Adesina",
        "subHeading": "",
        "alignment": "center"
    },
    "socials": {
        "apps": [
            "twitch": {
                "handle": "",
                "url": "",
            },
            "instagram": {
                "handle": "",
                "url": "https://www.instagram.com/",
            },
            "twitter": {
                "handle": "TimmyAdesina",
                "url": "https://twitter.com/TimmyAdesina",
            },
            "facebook": {
                "handle": "",
                "url": "https://web.facebook.com/",
            },
            "github": {
                "handle": "TimiBolu",
                "url": "https://github.com/TimiBolu",
            },
            "linkedin": {
                "handle": "timiadesina",
                "url": "https://www.linkedin.com/in/timiadesina",
            },
            "tiktok": {
                "handle": "",
                "url": "https://www.tiktok.com/@",
            },
            "dribbble": {
                "handle": "",
                "url": "https://dribbble.com/",
            },
            "behance": {
                "handle": "",
                "url": "https://www.behance.net/",
            },
            "snapchat": {
                "handle": "",
                "url": "https://www.snapchat.com/add/",
            },
            "email": {
                "handle": "adesinatim@gmail.com",
                "url": "mailto:adesinatim@gmail.com",
            },
            "whatsapp": {
                "handle": "09066744819",
                "url": "https://wa.me/09066744819",
            },
            "webaddress": {
                "handle": "",
                "url": "//",
            }
        ],
        "iconStyle": "outline",
        "alignment": "center"
    },
    "form": {
        "title": "",
        "description": "",
        "field1": "",
        "field2": "",
        "label": ""
    },
    "layout": "img-top"
}
FerX commented 2 months ago

ok, the issue is "label" property into form..

try this:

"form": {
type:"object",
props:{
    "title": {
        "type": "string"
    },
    "description": {
        "type": "string"
    },
    "label": {
        "type": "string"
    }
}
}
FerX commented 2 months ago

the pull request https://github.com/icebob/fastest-validator/pull/345 fix also this bug.

TimiBolu commented 2 months ago

Thank you so much. I really appreciate it. I actually wrote a very light wrapper for my use case with a joi/zod like syntax that compiles to fastest-validator schema. I have discovered writing the json schema like syntax can be quite daunting so I now auto generate them. It kind of allows me get the best of both worlds. I feel this may be a good way forward as an api extension for users who may be interested.

thib3113 commented 5 days ago

@TimiBolu can we close this issue ?

TimiBolu commented 5 days ago

Sure 👍