Giter VIP home page Giter VIP logo

Comments (3)

papajuans avatar papajuans commented on May 10, 2024

Joi will automatically try to do type conversions for you: https://github.com/spumko/joi#skip-conversions

from joi.

runrightfast avatar runrightfast commented on May 10, 2024

Right, but that is not the issue. The issue is that Array.includes() isn't working as expected. For example, the following schema doesn't validate properly (but it should) against the objects listed below :

var schema = { array : joi.types.Array().includes(joi.types.String().min(5), joi.types.Number().min(0)) };

The above schema reads that the object can contain an 'array' key whose type is an Array which can include array values of type String (with min length of 5 chars) or numbers >= 0.

With the above schema, I expect the the following objects to be valid, but Joi says that are invalid against the above schema:

{"array":[3]} : Error: the value of array must be a string, the value of array must be at least 5 characters long

{"array":["12345",3]} : Error: the value of array must be a string, the value of array must be at least 5 characters long

I wrote another test, which implies that when (at least) when Number and String are included together, Number is being ignored:

it.only('can validate that its elements only include certain types', function(done) {
var schema1 = {
array : joi.types.Array().includes(joi.types.String().min(5), joi.types.Number().min(0))
};

var schema2 = {
    array : joi.types.Array().includes(joi.types.Number().min(0), joi.types.String().min(5))
};

var errors = [];

var obj = {
    array : [ '12345' ]
};
var err = joi.validate(obj, schema1);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema1 : ' + err));
}
err = joi.validate(obj, schema2);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema2 : ' + err));
}

obj = {
    array : [ '1234' ]
};
err = joi.validate(obj, schema1);
if (!err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema1 : Should be invalid because it contains a String with length < 5'));
}
err = joi.validate(obj, schema2);
if (!err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema2 : Should be invalid because it contains a String with length < 5'));
}

obj = {
    array : [ 3 ]
};
err = joi.validate(obj, schema1);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema1 : ' + err));
}
err = joi.validate(obj, schema2);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema2 : ' + err));
}

obj = {
    array : [ '12345', 3 ]
};
err = joi.validate(obj, schema1);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema1 : ' + err));
}
err = joi.validate(obj, schema2);
if (err) {
    errors.push(new Error(JSON.stringify(obj) + ' : schema2 : ' + err));
}

if (errors.length > 0) {
    done(new Error(errors.join('\n')));
} else {
    done();
}

});

The test output is:

  1. Validator Domain Joi Array Type can validate that its elements only include certain types:
    Error: Error: {"array":[3]} : schema1 : Error: the value of array must be a string, the value of array must be at least 5 characters long
    Error: {"array":[3]} : schema2 : Error: the value of array must be a string, the value of array must be at least 5 characters long
    Error: {"array":["12345",3]} : schema1 : Error: the value of array must be a string, the value of array must be at least 5 characters long
    Error: {"array":["12345",3]} : schema2 : Error: the value of array must be a string, the value of array must be at least 5 characters long

from joi.

runrightfast avatar runrightfast commented on May 10, 2024

The issue is that validation works at the type level, but not via Joi.validate

here's one of Joi's tests from array.js:

it('should validate array of mixed Numbers & Strings', function (done) {

            verifyBehavior(A().includes(N(), S()), [
                [
                    [1, 2, 3],
                    true
                ],
                [
                    [50, 100, 1000],
                    true
                ],
                [
                    [1, 'a', 5, 10],
                    true
                ],
                [
                    ['walmart', 'everydaylowprices', 5000],
                    true
                ]
            ], done);
        });

It passes just fine.

here's another version of the test, except that now the array types are part of an object schema:

it.skip('should validate array of mixed Numbers & Strings', function() {
var A = joi.types.Array, N = joi.types.Number, S = joi.types.String;

        var schema = {
            a : A().includes(N(), S())
        };
        var error;
        error = joi.validate({
            a : [ 1, 2, 3 ]
        }, schema);
        if (error) {
            throw new Error('[ 1, 2, 3 ]: ' + error);
        }

        error = joi.validate({
            a : [ 50, 100, 1000 ]
        }, schema);
        if (error) {
            throw new Error('[ 50, 100, 1000 ]:' + error);
        }

        error = joi.validate({
            a : [ 1, 'a', 5, 10 ]
        }, schema);
        if (error) {
            throw new Error("[ 1, 'a', 5, 10 ]: " + error);
        }

        error = joi.validate({
            a : [ 'walmart', 'everydaylowprices', 5000 ]
        }, schema);
        if (error) {
            throw new Error("[ 'walmart', 'everydaylowprices', 5000 ]: " + error);
        }

    });

The test fails with the following error:

  1. Validator Domain Joi Array Type should validate array of mixed Numbers & Strings:
    Error: [ 1, 2, 3 ]: Error: the value of a must be a string, the value of a must be a string, the value of a must be a string

from joi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.