Add 'fastest-validator' to Cloudflare Pages functions


I have a perfect working Cloudlfare Pages environment with functions. The different API endpoints work flawless. Every endpoint accepts JSON, does some work and responds with JSON.

Now I want to extend the endpoints with some schema validation. For that I want to use fastest-validator, which as far I can see on this page is supported by Cloudflare, although it’s on Workers, but I trust this is the same for Pages with functions.

I added this line to one of the endpoints

import Validator from 'fastest-validator';

And further down the request

        const v = new Validator();
        const schema = {
            id: { type: 'number', positive: true, integer: true },
            name: { type: 'string', min: 3, max: 255 }
        const check = v.compile(schema);

My local “npx wrangler pages dev” chokes on the v.compile but thats due to some CSP constrains, so I guess I have to add a CSP-rule to allow ‘unsafe-eval’ for script-src. But the import works locally.

Just for testing I pushed the intermediate code to my Github repo. The build failed with:

19:54:16.526	✘ [ERROR] Could not resolve "fastest-validator"
19:54:16.526	    ../../../buildhome/repo/functions/api/contact.ts:1:22:
19:54:16.526	      1 │ import Validator from 'fastest-validator';
19:54:16.526	        ╵                       ~~~~~~~~~~~~~~~~~~~
19:54:16.526	  You can mark the path "fastest-validator" as external to exclude it from the bundle, which will remove this error.
19:54:16.529	1 error(s) and 0 warning(s) when compiling Worker.
19:54:16.531	Build failed with 1 error:

So the real question is, how to include ‘fastest-validator’ into a Pages function

My bad, forget to include the ‘node_modules’ dir in the repo