AJV's validator returns always true value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I need to validate JSON files in following way:



const setupSchema = fs.readFileSync(schemaDir +'/setup.json');


and compiling:



const setupValidator = ajv.compile(setupSchema);


My issue is that line:



console.log( setupValidator('') );


Always returns true even as validator's parameter is empty string like above. I suppose that the way of loading is bad but... need ask smarter people than me.










share|improve this question

























  • I may help if you could post the JSON schema too

    – customcommander
    Jan 3 at 20:08


















2















I need to validate JSON files in following way:



const setupSchema = fs.readFileSync(schemaDir +'/setup.json');


and compiling:



const setupValidator = ajv.compile(setupSchema);


My issue is that line:



console.log( setupValidator('') );


Always returns true even as validator's parameter is empty string like above. I suppose that the way of loading is bad but... need ask smarter people than me.










share|improve this question

























  • I may help if you could post the JSON schema too

    – customcommander
    Jan 3 at 20:08














2












2








2








I need to validate JSON files in following way:



const setupSchema = fs.readFileSync(schemaDir +'/setup.json');


and compiling:



const setupValidator = ajv.compile(setupSchema);


My issue is that line:



console.log( setupValidator('') );


Always returns true even as validator's parameter is empty string like above. I suppose that the way of loading is bad but... need ask smarter people than me.










share|improve this question
















I need to validate JSON files in following way:



const setupSchema = fs.readFileSync(schemaDir +'/setup.json');


and compiling:



const setupValidator = ajv.compile(setupSchema);


My issue is that line:



console.log( setupValidator('') );


Always returns true even as validator's parameter is empty string like above. I suppose that the way of loading is bad but... need ask smarter people than me.







javascript validation ajv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:51







Tomasz Waszczyk

















asked Jan 3 at 16:37









Tomasz WaszczykTomasz Waszczyk

55821227




55821227













  • I may help if you could post the JSON schema too

    – customcommander
    Jan 3 at 20:08



















  • I may help if you could post the JSON schema too

    – customcommander
    Jan 3 at 20:08

















I may help if you could post the JSON schema too

– customcommander
Jan 3 at 20:08





I may help if you could post the JSON schema too

– customcommander
Jan 3 at 20:08












2 Answers
2






active

oldest

votes


















2














From the quick start guide: (http://json-schema.org/)




The JSON document being validated or described we call the instance,
and the document containing the description is called the schema.



The most basic schema is a blank JSON object, which constrains
nothing, allows anything, and describes nothing:



{}



You can apply constraints on an instance by adding validation keywords
to the schema. For example, the “type” keyword can be used to restrict
an instance to an object, array, string, number, boolean, or null:



{ "type": "string" }




This means that if your schema is either an empty object or does not use the JSON Schema vocabulary, Ajv's compile function will always generate a validation function that always passes:



var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true});

var schema = {
foo: 'bar',
bar: 'baz',
baz: 'baz'
};

var validate = ajv.compile(schema);

validate({answer: 42}); //=> true
validate('42'); //=> true
validate(42); //=> true


Perhaps your setup.json is either incorrectly loaded or isn't a schema as per the JSON Schema specification.






share|improve this answer


























  • Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

    – Tomasz Waszczyk
    Jan 4 at 13:51



















1














// You should specify encoding while reading the file otherwise it will return raw buffer
const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
// setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
const setupValidator = ajv.compile(JSON.parse(setupSchema));
console.log( setupValidator('') ) // Now, this will return false;


Instead of doing above, you can just simply require the json file using require.



const setupSchema = require(schemaDir +'/setup.json');
const setupValidator = ajv.compile(setupSchema);
console.log( setupValidator('') );





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026332%2fajvs-validator-returns-always-true-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    From the quick start guide: (http://json-schema.org/)




    The JSON document being validated or described we call the instance,
    and the document containing the description is called the schema.



    The most basic schema is a blank JSON object, which constrains
    nothing, allows anything, and describes nothing:



    {}



    You can apply constraints on an instance by adding validation keywords
    to the schema. For example, the “type” keyword can be used to restrict
    an instance to an object, array, string, number, boolean, or null:



    { "type": "string" }




    This means that if your schema is either an empty object or does not use the JSON Schema vocabulary, Ajv's compile function will always generate a validation function that always passes:



    var Ajv = require('ajv');
    var ajv = new Ajv({allErrors: true});

    var schema = {
    foo: 'bar',
    bar: 'baz',
    baz: 'baz'
    };

    var validate = ajv.compile(schema);

    validate({answer: 42}); //=> true
    validate('42'); //=> true
    validate(42); //=> true


    Perhaps your setup.json is either incorrectly loaded or isn't a schema as per the JSON Schema specification.






    share|improve this answer


























    • Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

      – Tomasz Waszczyk
      Jan 4 at 13:51
















    2














    From the quick start guide: (http://json-schema.org/)




    The JSON document being validated or described we call the instance,
    and the document containing the description is called the schema.



    The most basic schema is a blank JSON object, which constrains
    nothing, allows anything, and describes nothing:



    {}



    You can apply constraints on an instance by adding validation keywords
    to the schema. For example, the “type” keyword can be used to restrict
    an instance to an object, array, string, number, boolean, or null:



    { "type": "string" }




    This means that if your schema is either an empty object or does not use the JSON Schema vocabulary, Ajv's compile function will always generate a validation function that always passes:



    var Ajv = require('ajv');
    var ajv = new Ajv({allErrors: true});

    var schema = {
    foo: 'bar',
    bar: 'baz',
    baz: 'baz'
    };

    var validate = ajv.compile(schema);

    validate({answer: 42}); //=> true
    validate('42'); //=> true
    validate(42); //=> true


    Perhaps your setup.json is either incorrectly loaded or isn't a schema as per the JSON Schema specification.






    share|improve this answer


























    • Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

      – Tomasz Waszczyk
      Jan 4 at 13:51














    2












    2








    2







    From the quick start guide: (http://json-schema.org/)




    The JSON document being validated or described we call the instance,
    and the document containing the description is called the schema.



    The most basic schema is a blank JSON object, which constrains
    nothing, allows anything, and describes nothing:



    {}



    You can apply constraints on an instance by adding validation keywords
    to the schema. For example, the “type” keyword can be used to restrict
    an instance to an object, array, string, number, boolean, or null:



    { "type": "string" }




    This means that if your schema is either an empty object or does not use the JSON Schema vocabulary, Ajv's compile function will always generate a validation function that always passes:



    var Ajv = require('ajv');
    var ajv = new Ajv({allErrors: true});

    var schema = {
    foo: 'bar',
    bar: 'baz',
    baz: 'baz'
    };

    var validate = ajv.compile(schema);

    validate({answer: 42}); //=> true
    validate('42'); //=> true
    validate(42); //=> true


    Perhaps your setup.json is either incorrectly loaded or isn't a schema as per the JSON Schema specification.






    share|improve this answer















    From the quick start guide: (http://json-schema.org/)




    The JSON document being validated or described we call the instance,
    and the document containing the description is called the schema.



    The most basic schema is a blank JSON object, which constrains
    nothing, allows anything, and describes nothing:



    {}



    You can apply constraints on an instance by adding validation keywords
    to the schema. For example, the “type” keyword can be used to restrict
    an instance to an object, array, string, number, boolean, or null:



    { "type": "string" }




    This means that if your schema is either an empty object or does not use the JSON Schema vocabulary, Ajv's compile function will always generate a validation function that always passes:



    var Ajv = require('ajv');
    var ajv = new Ajv({allErrors: true});

    var schema = {
    foo: 'bar',
    bar: 'baz',
    baz: 'baz'
    };

    var validate = ajv.compile(schema);

    validate({answer: 42}); //=> true
    validate('42'); //=> true
    validate(42); //=> true


    Perhaps your setup.json is either incorrectly loaded or isn't a schema as per the JSON Schema specification.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 8:16

























    answered Jan 3 at 20:20









    customcommandercustomcommander

    2,49411225




    2,49411225













    • Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

      – Tomasz Waszczyk
      Jan 4 at 13:51



















    • Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

      – Tomasz Waszczyk
      Jan 4 at 13:51

















    Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

    – Tomasz Waszczyk
    Jan 4 at 13:51





    Yes. I forgot about: const setupSchema = JSON.parse(fs.readFileSync(schemaDir +'/setup.json'));

    – Tomasz Waszczyk
    Jan 4 at 13:51













    1














    // You should specify encoding while reading the file otherwise it will return raw buffer
    const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
    // setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
    const setupValidator = ajv.compile(JSON.parse(setupSchema));
    console.log( setupValidator('') ) // Now, this will return false;


    Instead of doing above, you can just simply require the json file using require.



    const setupSchema = require(schemaDir +'/setup.json');
    const setupValidator = ajv.compile(setupSchema);
    console.log( setupValidator('') );





    share|improve this answer




























      1














      // You should specify encoding while reading the file otherwise it will return raw buffer
      const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
      // setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
      const setupValidator = ajv.compile(JSON.parse(setupSchema));
      console.log( setupValidator('') ) // Now, this will return false;


      Instead of doing above, you can just simply require the json file using require.



      const setupSchema = require(schemaDir +'/setup.json');
      const setupValidator = ajv.compile(setupSchema);
      console.log( setupValidator('') );





      share|improve this answer


























        1












        1








        1







        // You should specify encoding while reading the file otherwise it will return raw buffer
        const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
        // setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
        const setupValidator = ajv.compile(JSON.parse(setupSchema));
        console.log( setupValidator('') ) // Now, this will return false;


        Instead of doing above, you can just simply require the json file using require.



        const setupSchema = require(schemaDir +'/setup.json');
        const setupValidator = ajv.compile(setupSchema);
        console.log( setupValidator('') );





        share|improve this answer













        // You should specify encoding while reading the file otherwise it will return raw buffer
        const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
        // setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
        const setupValidator = ajv.compile(JSON.parse(setupSchema));
        console.log( setupValidator('') ) // Now, this will return false;


        Instead of doing above, you can just simply require the json file using require.



        const setupSchema = require(schemaDir +'/setup.json');
        const setupValidator = ajv.compile(setupSchema);
        console.log( setupValidator('') );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 17:54









        Surendra Kumar BSurendra Kumar B

        211




        211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026332%2fajvs-validator-returns-always-true-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]