jeffbski / joi-date-extensions-browser

joi-date-extensions bundled for the browser
Other
1 stars 2 forks source link

"base" must be an instance of "Joi object" error when trying to extend Joi #1

Open jeffbski opened 7 years ago

jeffbski commented 7 years ago

In following the joi-date-extensions usage instructions, I get the following error when trying to use the bundled version.

Uncaught Error: [
  {
    "name": "date",
    "language": {
      "format": "must be a string with one of the following formats {{format}}"
    },
    "coerce": function coerce(value, state, options) {\n\n\t\t        if (!value || value instanceof Date || typeof value === 'number') {\n\t\t            return value;\n\t\t        }\n\n\t\t        if (options.convert && this._flags.momentFormat) {\n\t\t            var date = Moment(value, this._flags.momentFormat, true);\n\t\t            if (date.isValid()) {\n\t\t                return date.toDate();\n\t\t            }\n\n\t\t            return this.createError('date.format', { value: value, format: this._flags.momentFormat }, state, options);\n\t\t        }\n\n\t\t        return value;\n\t\t    },
    "rules": [
      {
        "name": "format",
        "description": function description(params) {\n\n\t\t            return 'Date should respect format ' + params.format;\n\t\t        },
        "params": {
          "format": {
            "isJoi": true,
            "_type": "array",
            "_settings": null,
            "_valids": {
              "_set": []
            },
            "_invalids": {
              "_set": []
            },
            "_tests": [],
            "_refs": [],
            "_flags": {
              "sparse": false,
              "single": true,
              "presence": "required"
            },
            "_description": null,
            "_unit": null,
            "_notes": [],
            "_tags": [],
            "_examples": [],
            "_meta": [],
            "_inner": {
              "items": [
                {
                  "isJoi": true,
                  "_type": "string",
                  "_settings": null,
                  "_valids": {
                    "_set": []
                  },
                  "_invalids": {
                    "_set": [
                      ""
                    ]
                  },
                  "_tests": [],
                  "_refs": [],
                  "_flags": {},
                  "_description": null,
                  "_unit": null,
                  "_notes": [],
                  "_tags": [],
                  "_examples": [],
                  "_meta": [],
                  "_inner": {}
                }
              ],
              "ordereds": [],
              "inclusions": [
                {
                  "isJoi": true,
                  "_type": "string",
                  "_settings": null,
                  "_valids": {
                    "_set": []
                  },
                  "_invalids": {
                    "_set": [
                      ""
                    ]
                  },
                  "_tests": [],
                  "_refs": [],
                  "_flags": {},
                  "_description": null,
                  "_unit": null,
                  "_notes": [],
                  "_tags": [],
                  "_examples": [],
                  "_meta": [],
                  "_inner": {}
                }
              ],
              "exclusions": [],
              "requireds": []
            }
          }
        },
        "setup": function setup(params) {\n\n\t\t            this._flags.momentFormat = params.format;\n\t\t        },
        "validate": function validate(params, value, state, options) {\n\n\t\t            // No-op just to enable description\n\t\t            return value;\n\t\t        }
      }
    ],
    "base" [1]: {
      "isJoi": true,
      "_type": "date",
      "_settings": null,
      "_valids": {
        "_set": []
      },
      "_invalids": {
        "_set": []
      },
      "_tests": [],
      "_refs": [],
      "_flags": {},
      "_description": null,
      "_unit": null,
      "_notes": [],
      "_tags": [],
      "_examples": [],
      "_meta": [],
      "_inner": {}
    }
  }
]

[1] "base" must be an instance of "Joi object"
    at Object.exports.process (http://localhost:3007/dist/main.js:5668:19)
    at _class._validateWithOptions (http://localhost:3007/dist/main.js:5246:31)
    at _class.root.validate (http://localhost:3007/dist/main.js:283:25)
    at _class.root.attempt (http://localhost:3007/dist/main.js:311:29)
    at _class.root.assert (http://localhost:3007/dist/main.js:306:16)
    at _class.root.extend (http://localhost:3007/dist/main.js:365:16)
    at Object.<anonymous> (http://localhost:3007/dist/main.js:66:20)
    at __webpack_require__ (http://localhost:3007/dist/main.js:30:30)
    at Object.<anonymous> (http://localhost:3007/dist/main.js:57:19)
    at __webpack_require__ (http://localhost:3007/dist/main.js:30:30)
jeffbski commented 7 years ago

@Marsup would you have any idea why this is occurring and any ideas towards resolving it?

jeffbski commented 7 years ago

I have an example in example/with-ext that reproduces it.

npm install # main
npm run prepublish # build dist
cd examples/with-ext
npm install # with-ext
npm start # see error in console
Marsup commented 7 years ago

Simple, you have 2 jois in your bundle, instanceof doesn't work if you don't reference the same constructor. One seems to be ES5, the other ES6, there's probably something wrong in your build.

jeffbski commented 7 years ago

Thanks for responding. I'll take a closer look based on what you have pointed out.

jeffbski commented 7 years ago

No matter what I do, I can't figure out how to get webpack to load only one instance of joi. I've tried everything I can think of with no luck.

I am using the webpack-bundle-size-analyzer to look inside the bundles and see what is going on.

As far as I can tell, both joi-browser and joi-date-extension-browser are doing the right thing.

./node_modules/.bin/webpack --json | webpack-bundle-size-analyzer

It's when we try to bring them together in the example, when things get messed up. There seems to be issues getting the same module using the aliasing mechanism.

Currently it is bundling in both joi-browser and joi even though the package.json browser alias field tells it to load only joi-browser.

I've modified things to where I can get it to only have joi-browser but in that case the size is doubled so it is still loading two instances of the code.

I'm running out of ideas, so maybe someone with more webpack expertise can figure out what is going on.