-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 4 commits
6e9cc05
c479a44
a4f7543
410b564
dbc81e1
5666aec
0e3bc0e
025f91b
628b8cb
30b745d
2bc9962
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
}); | ||
}); | ||
}); |
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( | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolute nitpick: You could just skip the |
||
return null; | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this also make use of
peteharverson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource' | ||
}, | ||
|
@@ -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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this also make use of
peteharverson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
url: 'https://www.elastic.co/guide/en/elasticsearch/reference/{{version}}/ml-job-resource.html#ml-job-resource' | ||
}, | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.