Commit d5f79595 authored by Florent Chehab's avatar Florent Chehab
Browse files

usefull links validation ok

parent 3945371a
......@@ -5,13 +5,15 @@ USEFULL_LINKS_CONFIG = {
"url": {
"type": "url",
"required": True,
"validators": {}
"validators": {
"max_length": 300
}
},
"description": {
"type": "text",
"required": True,
"validators": {
"max_length": 500
"max_length": 50
}
}
}
......
......@@ -23,6 +23,9 @@ def validate_url(config, string):
validator_content = validators[validator]
if validator == 'extension':
validate_extension(validator_content, string)
elif validator == 'max_length':
if len(string) > validator_content:
raise ValidationError('Your url is too long !')
else:
raise Exception("Dev, you have implement something here...")
......
......@@ -15,6 +15,9 @@ import DeleteIcon from '@material-ui/icons/Delete';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import Field from './Field';
import Typography from '@material-ui/core/Typography';
import isUrl from '../../../../../utils/isUrl';
const styles = theme => ({
container: {
......@@ -33,8 +36,50 @@ class UsefulLinksField extends Field {
if (this.props.required && usefulLinks.length == 0) {
return true;
}
const notEmpty = usefulLinks.every(el => {
if (
(el.url != '' && el.description == '') ||
(el.url == '' && el.description != '')
) {
return false;
}
return true;
});
if (!notEmpty) { return true }
const urlValid = usefulLinks.every(el => {
if (!isUrl(el.url)) {
return false;
}
return true;
});
if (!urlValid) { return true }
const lengthOk = usefulLinks.every(el => {
if ((el.url && el.url.length > this.props.urlMaxLength) ||
(el.description && el.description.length > this.props.descriptionMaxLength)
) {
return false;
}
return true;
});
if (!lengthOk) { return true }
return false;
// TODO MORE
}
getValue() {
console.log("Get value comme il faut !")
const usefulLinks = this.state.value;
let emptyIndexes = Array();
usefulLinks.map((el, idx) => {
if (el.url == '' && el.description == '') {
emptyIndexes.push(idx);
}
})
console.log("empty indexes", emptyIndexes)
return usefulLinks.filter((s, idx) => !(idx in emptyIndexes));
}
updateUsefulLinks(newUsefulLinks) {
......@@ -101,6 +146,7 @@ class UsefulLinksField extends Field {
hasError={this.state.hasError}
label={this.props.label}
>
<Typography variant='caption'>Tous les champs doivent être remplis et les URLs doivent commencer par 'http' ou 'https' ou 'ftp'.</Typography>
<Grid
container
spacing={16}
......@@ -180,13 +226,17 @@ class UsefulLinksField extends Field {
UsefulLinksField.defaultProps = {
value: Array(),
urlMaxLength: 300,
descriptionMaxLength: 50,
}
UsefulLinksField.propTypes = {
UsefulLinksField.propTypes = {
value: PropTypes.arrayOf(PropTypes.shape({
url: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
})),
urlMaxLength: PropTypes.number.isRequired,
descriptionMaxLength: PropTypes.number.isRequired
};
export default compose(
......
const regexp = /^(?:(?:https?|ftp):\/\/)(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
export default function isUrl(str) {
return regexp.test(str);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment