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

[ML] Enhance job id error message #45349

Merged
merged 11 commits into from
Sep 17, 2019
7 changes: 6 additions & 1 deletion x-pack/legacy/plugins/ml/common/util/job_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import numeral from '@elastic/numeral';

import { ALLOWED_DATA_UNITS } from '../constants/validation';
import { parseInterval } from './parse_interval';
import { maxLengthValidator } from './validators';

// work out the default frequency based on the bucket_span in seconds
export function calculateDatafeedFrequencyDefaultSeconds(bucketSpanSeconds) {
Expand Down Expand Up @@ -223,7 +224,7 @@ export function mlFunctionToESAggregation(functionName) {
// Job name must contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores;
// it must also start and end with an alphanumeric character'
export function isJobIdValid(jobId) {
return (jobId.match(/^[a-z0-9\-\_]{1,64}$/g) && !jobId.match(/^([_-].*)?(.*[_-])?$/g)) ? true : false;
return (jobId.match(/^[a-z0-9\-\_]+$/g) && !jobId.match(/^([_-].*)?(.*[_-])?$/g)) ? true : false;
}

// To get median data for jobs and charts we need to use Elasticsearch's
Expand Down Expand Up @@ -271,13 +272,17 @@ export function basicJobValidation(job, fields, limits, skipMmlChecks = false) {
let valid = true;

if (job) {
const JOB_ID_MAX_LENGTH = 64;
// Job details
if (_.isEmpty(job.job_id)) {
messages.push({ id: 'job_id_empty' });
valid = false;
} else if (isJobIdValid(job.job_id) === false) {
messages.push({ id: 'job_id_invalid' });
valid = false;
} else if (maxLengthValidator(JOB_ID_MAX_LENGTH)(job.job_id)) {
messages.push({ id: 'invalid_max_length', maxLength: JOB_ID_MAX_LENGTH });
valid = false;
} else {
messages.push({ id: 'job_id_valid' });
}
Expand Down
22 changes: 22 additions & 0 deletions x-pack/legacy/plugins/ml/common/util/validators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { maxLengthValidator } from './validators';

describe('maxLengthValidator', () => {
test('should allow a valid input', () => {
expect(maxLengthValidator(2)('xx')).toBe(null);
});

test('should describe an invalid input', () => {
expect(maxLengthValidator(3)('example')).toEqual({
maxLength: {
requiredLength: 3,
actualLength: 7,
},
});
});
});
26 changes: 26 additions & 0 deletions x-pack/legacy/plugins/ml/common/util/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

/**
* Provides a validator function for maximum allowed input length.
* @param maxLength Maximum length allowed.
*/
export function maxLengthValidator(
Copy link
Member

@jgowdyelastic jgowdyelastic Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is now used as falsy check, the contents of the returned object are ignored.
Maybe it should be changed to just return a boolean.

Copy link
Member

@jgowdyelastic jgowdyelastic Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised that there is a string_utils.js file along side this file. IMO, this function is worth moving inside there, as it is validating the length of a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it as falsy check because of the current code structure. Validator function provides useful info to render error messages as well as error key which lets generate an object with all errors for specific field/model.

maxLength: number
): (value: string) => { maxLength: { requiredLength: number; actualLength: number } } | null {
return value => {
if (value && value.length > maxLength) {
return {
maxLength: {
requiredLength: maxLength,
actualLength: value.length,
},
};
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolute nitpick: You could just skip the else and return without it. For reference, here's the section on "return early from function" https://github.com/elastic/kibana/blob/master/style_guides/js_style_guide.md#returnthrow-early-from-functions

return null;
}
};
}
14 changes: 12 additions & 2 deletions x-pack/legacy/plugins/ml/server/models/job_validation/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,24 @@ export const getMessages = () => {
}),
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource'
},
invalid_max_length: {
status: 'ERROR',
text: i18n.translate('xpack.ml.validation.messages.invalidMaxLengthErrorMessage', {
defaultMessage: 'The max length is {maxLength, plural, one {# character} other {# characters}}.',
values: {
maxLength: '{{maxLength}}',
},
}),
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource'
},
job_id_valid: {
status: 'SUCCESS',
heading: i18n.translate('xpack.ml.models.jobValidation.messages.jobIdValidHeading', {
defaultMessage: 'Job id format is valid',
}),
text: i18n.translate('xpack.ml.models.jobValidation.messages.jobIdValidMessage', {
defaultMessage: 'Lowercase alphanumeric (a-z and 0-9) characters, hyphens or underscores, ' +
'starts and ends with an alphanumeric character.',
'starts and ends with an alphanumeric character, and is less than 64 characters.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also make use of JOB_ID_MAX_LENGTH?

}),
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource'
},
Expand All @@ -278,7 +288,7 @@ export const getMessages = () => {
}),
text: i18n.translate('xpack.ml.models.jobValidation.messages.jobGroupIdValidMessage', {
defaultMessage: 'Lowercase alphanumeric (a-z and 0-9) characters, hyphens or underscores, ' +
'starts and ends with an alphanumeric character.',
'starts and ends with an alphanumeric character, and is less than 64 characters.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also make use of JOB_ID_MAX_LENGTH?

}),
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource'
},
Expand Down