json-schema-yup-transform icon indicating copy to clipboard operation
json-schema-yup-transform copied to clipboard

required?.join is not a function

Open matpen opened this issue 2 years ago • 2 comments

After upgrading from 1.6.8 to 1.6.11 the following error happens in my project:

number.js:123 Uncaught (in promise) TypeError: required?.join is not a function
    at createBaseNumberSchema (number.js:123:1)
    at Object.createNumberSchema [as number] (number.js:41:1)
    at getValidationSchema (index.js:56:1)
    at Object.createValidationSchema [as default] (index.js:86:1)
    at push.50253.exports.buildProperties (index.js:60:1)
    at push.50253.exports.build (index.js:195:1)
    at push.50253.exports.buildProperties (index.js:29:1)
    at push.50253.exports.build [as default] (index.js:195:1)
    at convertToYup (index.js:31:1)

I tried to map the bug to the source, with the following results:

  • node_modules/json-schema-yup-transformer/dist/yup/schemas/number.d.ts contains this line export declare const createBaseNumberSchema: (yupSchema: Yup.NumberSchema, [key, value]: SchemaItem, required: JSONSchema["required"]) => Yup.NumberSchema; Since I never used typescript before, I am not sure about this, but the code looks like JSONSchema["required"] is supposed to extract the required property from the given schema: this also conforms to the variable naming, and the fact that the required variable is probably supposed to contain an array (because we are calling join());
  • however, the generated code (in node_modules/json-schema-yup-transformer/dist/yup/schemas/number.js line 35) looks like const createNumberSchema = ([key, value], required) => { ... }: therefore required ends up to be the original JSON schema, which is an object, not an array;
  • modifying that to const createNumberSchema = ([key, value], { required }) => { ... } seem to fix the problem, but many similar errors appear (for example in createStringSchema());

Can it be that something went wrong with the transpilation during the release process?

matpen avatar Aug 17 '22 17:08 matpen

@matpen can you provide a repro pls? Happy for code sandbox or to that ilk that isolates the issue. Thanks.

ritchieanesco avatar Aug 20 '22 00:08 ritchieanesco

Same error here.

My stacktrace (I add the directories)

number.ts:168 Uncaught TypeError: required.join is not a function
    at createBaseNumberSchema (number.ts:168:40)   src/yup/schemas/
    at Object.createNumberSchema [as number] (number.ts:28:10) src/yup/schemas/
    at getValidationSchema (index.ts:84:20) src/yup/schemas/
    at Object.createValidationSchema [as default] (index.ts:124:12) src/yup/schemas/
    at exports.buildProperties (index.ts:75:25)  src/yup/builder/
    at exports.build [as default] (index.ts:285:16)    src/yup/builder/
    at convertToYup (index.ts:15:25)  src/

I add my schema and my config to reproduce:

    const jsonSchema =  {
        "type": "object",
        "required": [
            "OficialCuenta",
            "tenencia",
            "canal"
        ],
        "properties": {
            "tenencia": {
                "title": "Tenencia",
                "type": "number",
                "default": 0
            },
            "canal": {
                "title": "Canal",
                "type": "string",
                "default": "",
                "pattern": "^.*$"
            },
            "OficialCuenta": {
                "title": "Oficial de cuenta",
                "type": "array",
                "default": [],
                "items": {
                    "type": "string",
                    "default": "",
                    "pattern": "^.*$"
                }
            },
            "Bienes_Personales": {
                "title": "Bienes personales",
                "type": "number"
            },
            "Relacion_Dependencia_Ingresos": {
                "title": "Relacion de dependencia - ingresos",
                "type": "number"
            },
            "Ganancias": {
                "title": "Ganancias",
                "type": "number"
            },
            "Monotributo_Autonomos_Ingresos": {
                "title": "Monotributo - autonomos - ingresos",
                "type": "number"
            },
            "Ingreso_Declarado": {
                "title": "Ingreso declarado",
                "type": "number"
            },
            "Ingresos_Balance": {
                "title": "Ingresos - balance",
                "type": "number",
                "examples": [
                    1234.56
                ],
                "default": 0
            },
            "Patrimonio_Neto_Balance": {
                "title": "Patrimonio neto - balance",
                "type": "number"
            },
            "Ingresos_Extraordinarios": {
                "title": "Ingresos extraordinarios",
                "type": "number"
            }
        }
    };

    const config = {
        "errors": {
            "defaults": {
                "required": "Requerido"
            }
        }
    };
   const yupschema = convertToYup(jsonschema, config);

leonbloy avatar Sep 02 '22 14:09 leonbloy

It also happens with schema defined in basic example.

keeema avatar Oct 10 '22 08:10 keeema

OK, I spent a little time on better determination and the last working version is 1.6.10, so some change which breaks it must be in 1.6.11.

Error occurs with sample:

const schema = {
  type: "object",
  $schema: "http://json-schema.org/draft-07/schema#",
  $id: "example",
  title: "Example",
  properties: {
    name: {
      type: "string"
    }
  }
};

If I change

name: {
   type: "string"
}```

to

```json
name: {
   type: "object"
}

then no error occurs.

Hope this helps.

keeema avatar Oct 10 '22 10:10 keeema

Thanks for looking into it @keeema. Will do some further investigation, the only changes from 1.6.11 and 1.6.10 are a couple of dependency version bumps. Will look into the compiled code to make sure its doing the correct checks.

ritchieanesco avatar Oct 29 '22 00:10 ritchieanesco

@keeema issue was due to transpilation as mentiioned by @matpen. Change was published in v1.6.12. Thanks for contributing!

ritchieanesco avatar Oct 29 '22 01:10 ritchieanesco