question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A `schemaType` of `integer` doesn't seem to work correctly with custom keyword

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.6.2

Ajv options object

{
    strict: true,
    allErrors: true,
    logger: console,
    loadSchema: () => {},
}

JSON Schema

{
    "myBadKeyword": 5,
}

Your code

const Ajv = require('ajv');

async function testIt() {
  const ajvOptions = {
    strict: true,
    allErrors: true,
    logger: console,
    loadSchema: () => {},
  };

  const ajv = new Ajv(ajvOptions);
  ajv.addKeyword({
    keyword: 'myBadKeyword',
    schemaType: 'integer',
  });

  ajv.addKeyword({
    keyword: 'myGoodKeyword',
    schemaType: 'number',
  });

  const myBadSchema = {
    myBadKeyword: 5,
  };

  const myGoodSchema = {
    myGoodKeyword: 5,
  };

  try {
    await ajv.compileAsync(myBadSchema);
    console.log('bad schema compiled without exception');
  } catch (e) {
    console.log(e);
  }

  try {
    await ajv.compileAsync(myGoodSchema);
    console.log('good schema compiled without exception');
  } catch (e) {
    console.log(e);
  }
}

testIt().then(console.log('done'));

Output:

✗ node test.js
done
Error: myBadKeyword value must be ["integer"]
    at new KeywordCxt (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:298:23)
    at keywordCode (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:449:17)
    at /Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:222:17
    at CodeGen.code (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:439:13)
    at CodeGen.block (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:568:18)
    at iterateKeywords (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:219:9)
    at groupKeywords (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:208:13)
    at /Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:192:13
    at CodeGen.code (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:439:13)
    at CodeGen.block (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:568:18)
good schema compiled without exception

Validation result, data AFTER validation, error messages

Compiling the good schema with a custom keyword using schemaType of number works fine. Compiling the bad schema with a custom keyword using schemaType of integer throws an exception even though the data in the schema is an integer.

What results did you expect?

I expected that I could use schemaType of integer in this case.

Are you going to resolve the issue?

I just started looking into the issue. Not familiar with the code base, so I’m not sure.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
epoberezkincommented, Jul 23, 2021

It probably should be supported, it just never was…

1reaction
epoberezkincommented, Jul 21, 2021

I don’t think “integer” was ever supported in schemaType. A simple workaround is to use “number” and further constrain using metaSchema: {type: “integer”} in keyword definition

Read more comments on GitHub >

github_iconTop Results From Across the Web

A `schemaType` of `integer` doesn't seem to work correctly with ...
A `schemaType` of `integer ` doesn't seem to work correctly with custom keyword.
Read more >
GraphQL schema basics
Your GraphQL server uses a schema to describe the shape of your available data. This schema defines a hierarchy of types with fields...
Read more >
JSON Schema - if then statement is always evaluated even ...
I have a JSON Schema, in the schema - when the property type is equal to menu , then the property default should...
Read more >
Schemas and Types - GraphQL
On this page, you'll learn all you need to know about the GraphQL type system and how it describes what data can be...
Read more >
Creating an Amazon EventBridge schema
There are minor differences in how keywords are handled, such as type and format . OpenAPI doesn't support JSONSchema Hyper-Schema hyperlinks in JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found