Skip to content
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

Why process format if type validation fails #125

Closed
whitlockjc opened this issue Aug 5, 2015 · 15 comments
Closed

Why process format if type validation fails #125

whitlockjc opened this issue Aug 5, 2015 · 15 comments

Comments

@whitlockjc
Copy link
Contributor

Imagine I've got a schema like this:

{
  "type": "integer",
  "format": "int32"
}

I register my int32 format validator and I validate the value 1.1. It comes to my surprise that instead of getting a type error alone, I get a type and a format error:

[
  {
    code: 'INVALID_TYPE',
    message: 'Expected type integer but found type number',
    path: []
  },
  {
    code: 'INVALID_FORMAT',
    message: 'Object didn\'t pass validation for format int64: 1.1',
    path: []
  }
]

That being said, why are we doing format validation on values that have failed type validation?

@zaggino zaggino added the bug label Aug 6, 2015
@zaggino
Copy link
Owner

zaggino commented Aug 6, 2015

yes, this should be fixed

@whitlockjc
Copy link
Contributor Author

I searched but didn't see a reported issue, my bad.

zaggino added a commit that referenced this issue Aug 12, 2015
@zaggino zaggino removed the bug label Aug 12, 2015
@zaggino
Copy link
Owner

zaggino commented Aug 12, 2015

Hi @whitlockjc , I put this test in place: https://github.com/zaggino/z-schema/blob/master/test/ZSchemaTestSuite/Issue125.js
But it seems it's working fine, without chaning the code of the validator. Any chance you're using the breakOnFirstError: false setting?

@whitlockjc
Copy link
Contributor Author

I'm not using breakOnFirstError so whatever its default is. Maybe I've got an old version of z-schema. Here is my test: https://github.com/apigee-127/sway/blob/master/test/test-2.0.js#L272 Looks like I'm using 3.12.0 of z-schema. Maybe it was just fixed after that.

@zaggino
Copy link
Owner

zaggino commented Aug 13, 2015

3.12.2 is the latest now, but I don't think I have changed anything to fix this.

Anyway, please make a PR to this test: https://github.com/zaggino/z-schema/blob/master/test/ZSchemaTestSuite/Issue125.js if you think I got it wrong somewhere. Closing this in the meantime.

@zaggino zaggino closed this as completed Aug 13, 2015
@whitlockjc
Copy link
Contributor Author

I cannot reproduce this with standalone z-schema. :) Seems my project is somehow busting things up. Not sure how but it is.

@whitlockjc
Copy link
Contributor Author

This is really baffling. Standalone z-schema works fine, z-schema in my environment fails. If you get a few minutes: https://github.com/apigee-127/sway/blob/master/test/test-2.0.js#L303 That test, and most of the ones in that same describe block, all have the same type+format error issue. If you've got a few minutes:

  • git clone https://github.com/apigee-127/sway.git
  • cd sway
  • npm install
  • gulp test-node

That shouldn't take more than a few minutes. If you see these tests pass, we have some sort of situation in my usage of z-schema where it violates the contract your new test show works in standalone z-schema.

@zaggino
Copy link
Owner

zaggino commented Aug 19, 2015

  174 passing (4s)

[10:05:47] Finished 'test-node' after 4.99 s

@zaggino
Copy link
Owner

zaggino commented Aug 19, 2015

Can you make a test fail so I can check what's wrong with z-schema in your case?

@whitlockjc
Copy link
Contributor Author

Here is a diff you can apply:

diff --git a/test/test-2.0.js b/test/test-2.0.js
index 71638c8..a5c103e 100644
--- a/test/test-2.0.js
+++ b/test/test-2.0.js
@@ -314,11 +314,6 @@ describe('sway (Swagger 2.0)', function () {
               code: 'INVALID_TYPE',
               message: 'Expected type integer but found type number',
               path: []
-            },
-            {
-              code: 'INVALID_FORMAT',
-              message: 'Object didn\'t pass validation for format int32: 1.1',
-              path: []
             }
           ]);
         });
@@ -373,11 +368,6 @@ describe('sway (Swagger 2.0)', function () {
               code: 'INVALID_TYPE',
               message: 'Expected type integer but found type number',
               path: []
-            },
-            {
-              code: 'INVALID_FORMAT',
-              message: 'Object didn\'t pass validation for format int64: 1.1',
-              path: []
             }
           ]);
         });

zaggino added a commit that referenced this issue Aug 19, 2015
@zaggino
Copy link
Owner

zaggino commented Aug 19, 2015

Ah, error was completely in a different place, fixed in 3.12.3

@whitlockjc
Copy link
Contributor Author

Glad I wasn't making it up. :) Thanks for your help.

@frederikprijck
Copy link

frederikprijck commented Jul 29, 2024

Hey @zaggino,

Sorry to revive this, but I noticed the same behavior is introduced again, I am running v6.0.1:

const ZSchema = require("z-schema");

ZSchema.registerFormat("test", (str) => {
  return typeof str === 'string' &&  str.indexOf("https") > -1;
});

const validator = new ZSchema();

const schema = {
  type: "object",
  properties: {
    callbacks: {
      type: "array",
      items: {
        type: "string",
        format: "test",
      },
    },
  },
};

validator.validate(
  {
    callbacks: [true],
  },
  schema,
  (data, err) => {
    console.log(data);
  }
);

This outputs:

[
  {
    code: 'INVALID_TYPE',
    params: [ 'string', 'boolean' ],
    message: 'Expected type string but found type boolean',
    path: '#/callbacks/0',
    schemaId: undefined,
    [Symbol(z-schema/schema)]: { format: 'test', type: 'string', '__$validated': true },
    [Symbol(z-schema/json)]: { callbacks: [Array] }
  },
  {
    code: 'INVALID_FORMAT',
    params: [ 'test', true ],
    message: "Object didn't pass validation for format test: true",
    path: '#/callbacks/0',
    schemaId: undefined,
    [Symbol(z-schema/schema)]: { format: 'test', type: 'string', '__$validated': true },
    [Symbol(z-schema/json)]: { callbacks: [Array] }
  }
]

Is this expected in v6?

Note that if I set breakOnFirstError to true it works again and it only logs the INVALID_TYPE but then I have the following issue:

validator.validate(
  {
    callbacks: ['a', 'b'],
  },
  schema,
  (data) => {
    // This now only logs 1 error
    console.log(data);
  }
);

The above ends up in only a single validation error when breakOnFirstError is set to true.

[
  {
    code: 'INVALID_FORMAT',
    params: [ 'test', 'b' ],
    message: "Object didn't pass validation for format test: b",
    path: '#/callbacks/1',
    schemaId: undefined,
    [Symbol(z-schema/schema)]: { format: 'test', type: 'string', '__$validated': true },
    [Symbol(z-schema/json)]: { callbacks: [Array] }
  }
]

I would want:

  • do not break on first error
  • but do not execute formatters when the type is invalid.

Is this possible with v6?

zaggino added a commit that referenced this issue Jul 29, 2024
@zaggino
Copy link
Owner

zaggino commented Jul 29, 2024

@frederikprijck Published a fix in z-schema@6.0.2

@frederikprijck
Copy link

Confirmed, that works 👍 Thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants