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

Ensure lat/lng are at least 2 decimal places #373

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/rules/format/lat-lng-precision-rule-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const LatLngPrecisionRule = require('./lat-lng-precision-rule');
const Model = require('../../classes/model');
const ModelNode = require('../../classes/model-node');
const ValidationErrorType = require('../../errors/validation-error-type');
const ValidationErrorSeverity = require('../../errors/validation-error-severity');

describe('LatLngPrecisionRule', () => {
const rule = new LatLngPrecisionRule();

const model = new Model({
type: 'GeoCoordinates',
fields: {
latitude: {
fieldName: 'latitude',
minDecimalPlaces: 2,
sameAs: 'https://schema.org/latitude',
requiredType: 'https://schema.org/Number',
},
longitude: {
fieldName: 'longitude',
minDecimalPlaces: 2,
sameAs: 'https://schema.org/longitude',
requiredType: 'https://schema.org/Number',
},
},
}, 'latest');
model.hasSpecification = true;

it('should target lat / long fields of GeoCoordinates', () => {
let isTargeted = rule.isFieldTargeted(model, 'latitude');
expect(isTargeted).toBe(true);

isTargeted = rule.isFieldTargeted(model, 'longitude');
expect(isTargeted).toBe(true);

isTargeted = rule.isFieldTargeted(model, 'type');
expect(isTargeted).toBe(false);
});

it('should return no error for a value above a minDecimalPlaces threshold', async () => {
const values = [
-89.123456,
5.01123,
70.445234,
];

for (const value of values) {
const data = {
latitude: value,
longitude: value,
};
const nodeToTest = new ModelNode(
'$',
data,
null,
model,
);
const errors = await rule.validate(nodeToTest);
expect(errors.length).toBe(0);
}
});
it('should return an error for a value below a minDecimalPlaces threshold', async () => {
const values = [
90.1,
-100.1,
110,
];

for (const value of values) {
const data = {
latitude: value,
longitude: value,
};
const nodeToTest = new ModelNode(
'$',
data,
null,
model,
);
const errors = await rule.validate(nodeToTest);
expect(errors.length).toBe(2);
expect(errors[0].type).toBe(ValidationErrorType.INVALID_PRECISION);
expect(errors[0].severity).toBe(ValidationErrorSeverity.FAILURE);
expect(errors[1].type).toBe(ValidationErrorType.INVALID_PRECISION);
expect(errors[1].severity).toBe(ValidationErrorSeverity.FAILURE);
}
});
});
69 changes: 69 additions & 0 deletions src/rules/format/lat-lng-precision-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const Rule = require('../rule');
const PrecisionHelper = require('../../helpers/precision');
const ValidationErrorType = require('../../errors/validation-error-type');
const ValidationErrorCategory = require('../../errors/validation-error-category');
const ValidationErrorSeverity = require('../../errors/validation-error-severity');

module.exports = class LatLngPrecisionRule extends Rule {
constructor(options) {
super(options);
this.targetFields = {
GeoCoordinates: ['latitude', 'longitude'],
};
this.meta = {
name: 'LatLngPrecisionRule',
description: 'Validates that latitude and longitude properties are to the correct number of decimal places.',
tests: {
belowMinimum: {
description: 'Raises a suggestion if a number\'s precision is below the minimum number of decimal places suggested for a property.',
message: 'The value of this property should have at least {{minDecimalPlaces}} decimal places. Note that this notice will also appear when trailing zeros have been truncated.',
sampleValues: {
minDecimalPlaces: 2,
},
category: ValidationErrorCategory.CONFORMANCE,
severity: ValidationErrorSeverity.FAILURE,
type: ValidationErrorType.INVALID_PRECISION,
},
},
};
}

validateField(node, field) {
// Don't do this check for models that we don't actually have a spec for
if (!node.model.hasSpecification) {
return [];
}
if (!node.model.hasField(field)) {
return [];
}

const errors = [];

// Get the field object
const fieldObj = node.model.getField(field);
const fieldValue = node.getMappedValue(field);

if (typeof fieldValue !== 'number') {
return [];
}

if (
typeof fieldObj.minDecimalPlaces !== 'undefined'
&& PrecisionHelper.getPrecision(fieldValue) < fieldObj.minDecimalPlaces
) {
errors.push(
this.createError(
'belowMinimum',
{
value: fieldValue,
path: node.getPath(field),
},
{
minDecimalPlaces: fieldObj.minDecimalPlaces,
},
),
);
}
return errors;
}
};