2019-12-04 23:54:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* @module
|
|
|
|
|
* Définit le modèle de données utilisé par l’application.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Types de termes existants.
|
|
|
|
|
*/
|
|
|
|
|
export const termTypes = {
|
|
|
|
|
disease: 'Maladie',
|
|
|
|
|
symptom: 'Symptôme',
|
|
|
|
|
sign: 'Signe',
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-05 01:38:18 +00:00
|
|
|
|
/**
|
|
|
|
|
* Crée un valideur de type de propriété React supportant l’extension
|
|
|
|
|
* `.isRequired` pour marquer une propriété comme requise.
|
|
|
|
|
*
|
|
|
|
|
* @param basicValidator Fonction basique de validation.
|
|
|
|
|
* @return Fonction augmentée supportant l’extension `.isRequired`.
|
|
|
|
|
*/
|
|
|
|
|
const makePropType = basicValidator =>
|
|
|
|
|
{
|
|
|
|
|
const validator = (props, propName, componentName) =>
|
|
|
|
|
{
|
|
|
|
|
// Version optionnelle : on n’appelle le valideur que si la propriété a
|
|
|
|
|
// été passée
|
|
|
|
|
if (propName in props)
|
|
|
|
|
{
|
|
|
|
|
return basicValidator(props, propName, componentName);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
validator.isRequired = (props, propName, componentName) =>
|
|
|
|
|
{
|
|
|
|
|
// Version obligatoire : on jette une erreur si la propriété n’a pas
|
|
|
|
|
// été passée puis on procède à la validation
|
|
|
|
|
if (!(propName in props))
|
|
|
|
|
{
|
|
|
|
|
return new Error(
|
|
|
|
|
`Missing prop “${propName}” in props supplied to `
|
|
|
|
|
+ `${componentName}.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return basicValidator(props, propName, componentName);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return validator;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-04 23:54:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Type de terme.
|
|
|
|
|
*/
|
|
|
|
|
export const TermType = PropTypes.oneOf(Object.values(termTypes));
|
|
|
|
|
|
2019-12-05 01:38:18 +00:00
|
|
|
|
/**
|
|
|
|
|
* URL.
|
|
|
|
|
*/
|
|
|
|
|
const URL = makePropType((props, propName, componentName) =>
|
|
|
|
|
{
|
|
|
|
|
const value = props[propName];
|
|
|
|
|
|
|
|
|
|
if (typeof value !== 'string')
|
|
|
|
|
{
|
|
|
|
|
return new Error(
|
|
|
|
|
`URL passed as ${componentName}’s ${propName} prop must be `
|
|
|
|
|
+ `a string, not a ${typeof value}.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!value.startsWith('http://') && !value.startsWith('https://'))
|
|
|
|
|
{
|
|
|
|
|
return new Error(
|
|
|
|
|
`URL “${value}”, passed as ${componentName}’s ${propName} `
|
2019-12-05 02:24:21 +00:00
|
|
|
|
+ 'prop, does not start with “http://” or “https://”.'
|
2019-12-05 01:38:18 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-04 23:54:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Terme.
|
|
|
|
|
*
|
|
|
|
|
* Peut être une maladie, un symptôme ou un signe.
|
|
|
|
|
*/
|
|
|
|
|
export const Term = PropTypes.exact({
|
2019-12-05 01:38:18 +00:00
|
|
|
|
// Identifiant unique
|
2019-12-04 23:54:44 +00:00
|
|
|
|
id: PropTypes.string.isRequired,
|
2019-12-05 01:38:18 +00:00
|
|
|
|
|
|
|
|
|
// Nom canonique
|
2019-12-04 23:54:44 +00:00
|
|
|
|
name: PropTypes.string.isRequired,
|
2019-12-05 01:38:18 +00:00
|
|
|
|
|
|
|
|
|
// Noms alternatifs
|
2019-12-04 23:54:44 +00:00
|
|
|
|
alias: PropTypes.arrayOf(PropTypes.string).isRequired,
|
2019-12-05 01:38:18 +00:00
|
|
|
|
|
|
|
|
|
// Types (maladie, symptôme, signe)
|
2019-12-04 23:54:44 +00:00
|
|
|
|
types: PropTypes.arrayOf(TermType).isRequired,
|
2019-12-05 01:38:18 +00:00
|
|
|
|
|
|
|
|
|
// Facteur d’importance
|
2019-12-04 23:54:44 +00:00
|
|
|
|
weight: PropTypes.number.isRequired,
|
2019-12-05 01:38:18 +00:00
|
|
|
|
|
|
|
|
|
// Adresse de la ressource de référence
|
|
|
|
|
url: URL.isRequired,
|
2019-12-04 23:54:44 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Relation entre deux termes.
|
|
|
|
|
*/
|
2019-12-05 02:24:21 +00:00
|
|
|
|
export const Relation = makePropType((props, propName) =>
|
2019-12-04 23:54:44 +00:00
|
|
|
|
{
|
|
|
|
|
const value = props[propName];
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(value))
|
|
|
|
|
{
|
|
|
|
|
return new Error(
|
|
|
|
|
`Relation ${propName} must be an array.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value.length !== 2)
|
|
|
|
|
{
|
|
|
|
|
return new Error(
|
|
|
|
|
`Relation ${propName} must contain exactly two entries.`
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-05 01:38:18 +00:00
|
|
|
|
});
|