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

Weird behavior regarding validation on "multipleOf" #2353

Closed
fujianyang opened this issue Dec 13, 2023 · 1 comment
Closed

Weird behavior regarding validation on "multipleOf" #2353

fujianyang opened this issue Dec 13, 2023 · 1 comment

Comments

@fujianyang
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
I have been trying with both 6.10.1 and 8.12.0 so far. Both of them have the same behavior regarding validation on properties that have "multipleOf" defined.

The problem is the validation falls on property value of "0.3" and "0.7" if "multipleOf" is defined as "0.1" for a property. It doesn't make sense to me. But I couldn't find out what I did wrong. Other values like "0.2", "0.4", etc... are fine.

Ajv options object
I have tried the following:

const ajv = new AJV();

JSON Schema

{
  "type": "object",
  "properties": {
    "myProperty": {
      "type": "number",
      "multipleOf": 0.1
    }
  }
}

Sample data

{
  "myProperty": 0.3
}

Your code


const AJV = require('ajv');
const fs = require('fs');
const path = require('path');

// Initialize AJV
const ajv = new AJV();

// Function to load JSON file
function loadJSONFile(filePath) {
  try {
    const absolutePath = path.resolve(filePath);
    const data = fs.readFileSync(absolutePath, 'utf8');
    return JSON.parse(data);
  } catch (err) {
    console.error(`Error reading file from disk: ${err}`);
    return null;
  }
}

// Validate inputs
if (process.argv.length !== 4) {
  console.log('Usage: node validate.js <schema_file> <data_file>');
  process.exit(1);
}

// Load schema and data from provided file paths
const [,, schemaFilePath, dataFilePath] = process.argv;

const schema = loadJSONFile(schemaFilePath);
const data = loadJSONFile(dataFilePath);

if (!schema || !data) {
  process.exit(1); // Exit if schema or data failed to load
}

// Compile the schema using AJV
const validate = ajv.compile(schema);

// Perform the validation
const valid = validate(data);


if (valid) {
  console.log('Validation successful!');
} else {
  console.log('Validation failed:');
  console.log(validate.errors);
}

Validation result, data AFTER validation, error messages

Validation failed:
[
  {
    instancePath: '/myProperty',
    schemaPath: '#/properties/myProperty/multipleOf',
    keyword: 'multipleOf',
    params: { multipleOf: 0.1 },
    message: 'must be multiple of 0.1'
  }
]


What results did you expect?
Validation successful!

Are you going to resolve the issue?

@fujianyang
Copy link
Author

See #84

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

No branches or pull requests

1 participant