wikimedica-disease-search/app/src/data/model.js

73 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @module
* Définit le modèle de données utilisé par lapplication.
*/
import PropTypes from 'prop-types';
/**
* Types de termes existants.
*/
export const termTypes = {
disease: 'Maladie',
symptom: 'Symptôme',
sign: 'Signe',
};
/**
* Type de terme.
*/
export const TermType = PropTypes.oneOf(Object.values(termTypes));
/**
* Terme.
*
* Peut être une maladie, un symptôme ou un signe.
*/
export const Term = PropTypes.exact({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
alias: PropTypes.arrayOf(PropTypes.string).isRequired,
types: PropTypes.arrayOf(TermType).isRequired,
weight: PropTypes.number.isRequired,
});
/**
* Relation entre deux termes.
*/
export const Relation = (props, propName, componentName) =>
{
if (!(propName in props))
{
return new Error(
`Missing ${propName} in props supplied to ${componentName}.`
);
}
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.`
);
}
if (
typeof value[0] !== 'string'
|| typeof value[1] !== 'string'
)
{
return new Error(
`Entries of relation ${propName} must be string IDs.`
);
}
};