Skip to content

Joi wrong value parsing #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
akaNightmare opened this issue Jul 29, 2016 · 6 comments
Closed

Joi wrong value parsing #964

akaNightmare opened this issue Jul 29, 2016 · 6 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@akaNightmare
Copy link

akaNightmare commented Jul 29, 2016

Context

  • node v6.2.0:
  • joi 9.0.0:
  • used with (expressjs):
const validator = Joi.object({
            name: Joi.string(),
            pricing_formula: Joi.string(),
            period: Joi.string().valid(Object.keys(this.model.PERIODS)).allow(null),
            is_active: Joi.boolean(),
            formula_variables: Joi.array().items(Joi.object({
                name: Joi.string(),
                type: Joi.string().valid(Object.keys(models.PricingFormulaVariable.TYPES)),
                value: Joi.alternatives().when('type', {
                    is: models.PricingFormulaVariable.TYPES.constant_map,
                    then: Joi.object({
                        default: Joi.number().integer().min(0)
                    }).required()/*.pattern(/[\w\d]+/, Joi.number().integer().min(0))*/,
                    otherwise: Joi.number().integer().min(0)
                }),
            })).unique((a, b) => a.name === b.name)
        }).options({
            allowUnknown: false,
            abortEarly: false,
            presence: 'required'
        });

const validatedBody = Joi.attempt({
                name: 'hello',
                pricing_formula: 'a + b + c',
                period: null,
                is_active: true,
                formula_variables: [
                    {
                        name: 'a',
                        type: 'variable',
                        value: 100
                    },
                    {
                        name: 'b',
                        type: 'constant',
                        value: 0
                    },
                    {
                        name: 'c',
                        type: 'constant_map',
                        value: '{}'
                    }
                ]
            }, validator);

Which result you had ?

Cannot create property '$miss$default|1$end$' on string '{}'

What did you expect ?

{  
   isJoi:true,
   name:"ValidationError",
   details:[  
      {  
         message:""         value" must be an object",
         path:"formula_variables.2.value",
         type:"object.base",
         context:{  
            key:"value"
         }
      }
   ],
   _object:{  
      name:"hello",
      pricing_formula:"a + b + c",
      period:null,
      is_active:true,
      formula_variables:[  
         {  
            name:"a",
            type:"variable",
            value:100
         },
         {  
            name:"b",
            type:"constant",
            value:0
         },
         {  
            name:"c",
            type:"constant_map",
            value:"{}"
         }
      ]
   }
}
@Marsup
Copy link
Collaborator

Marsup commented Jul 30, 2016

Works for me without all your models things you didn't provide, you'll have to give me something I can reproduce.

@akaNightmare
Copy link
Author

@Marsup just changed the value of name: "c" to '{}'

@Marsup Marsup added the bug Bug or defect label Sep 4, 2016
@kamronbatman
Copy link
Contributor

kamronbatman commented Sep 30, 2016

@akaNightmare In your validated body try changing:

value: '{}'

to

value: {}

@Marsup
Copy link
Collaborator

Marsup commented Sep 30, 2016

It'll indeed fix it but that's still a bug. I don't have time to work on it right now.

@kamronbatman
Copy link
Contributor

@Marsup I didn't realize Joi.object() is supposed to validate against stringified json/javascript?

@EvgenyOrekhov
Copy link
Contributor

Here is a simple case that reproduces the bug:

const Joi = require("joi");

const schema = Joi.object({
    key: Joi.object({
        allowedKey: Joi.string()
    })
});

Joi.attempt({key: `{"forbiddenKey":"value"}`}, schema);

// TypeError: Cannot assign to read only property
// '_$miss$_forbiddenKey|1_$end$_' of {"forbiddenKey":"value"}

This example on RunKit:
https://runkit.com/57d675ee7580c314003cf648/5890b26c58dc550014870467.

I hope it helps to fix it!

@Marsup Marsup closed this as completed in 8fb9ea8 Mar 26, 2017
Marsup added a commit that referenced this issue Mar 26, 2017
Marsup added a commit that referenced this issue Mar 26, 2017
@Marsup Marsup self-assigned this Mar 26, 2017
@Marsup Marsup added this to the 10.3.1 milestone Mar 26, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

4 participants