| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- import {Service} from '@e22m4u/service';
- import {RelationType} from './relation-type.js';
- import {RelationType as Type} from './relation-type.js';
- import {InvalidArgumentError} from '../../../errors/index.js';
- /**
- * Relations definition validator.
- */
- export class RelationsDefinitionValidator extends Service {
- /**
- * Validate.
- *
- * @param {string} modelName
- * @param {object} relDefs
- */
- validate(modelName, relDefs) {
- if (!modelName || typeof modelName !== 'string')
- throw new InvalidArgumentError(
- 'A first argument of RelationsDefinitionValidator.validate ' +
- 'should be a non-empty String, but %v given.',
- modelName,
- );
- if (!relDefs || typeof relDefs !== 'object' || Array.isArray(relDefs))
- throw new InvalidArgumentError(
- 'The provided option "relations" of the model %v ' +
- 'should be an Object, but %v given.',
- modelName,
- relDefs,
- );
- const relNames = Object.keys(relDefs);
- relNames.forEach(relName => {
- const relDef = relDefs[relName];
- this._validateRelation(modelName, relName, relDef);
- });
- }
- /**
- * Validate relation.
- *
- * @param {string} modelName
- * @param {string} relName
- * @param {object} relDef
- */
- _validateRelation(modelName, relName, relDef) {
- if (!modelName || typeof modelName !== 'string')
- throw new InvalidArgumentError(
- 'A first argument of RelationsDefinitionValidator._validateRelation ' +
- 'should be a non-empty String, but %v given.',
- modelName,
- );
- if (!relName || typeof relName !== 'string')
- throw new InvalidArgumentError(
- 'The relation name of the model %v should be ' +
- 'a non-empty String, but %v given.',
- modelName,
- relName,
- );
- if (!relDef || typeof relDef !== 'object' || Array.isArray(relDef))
- throw new InvalidArgumentError(
- 'The relation %v of the model %v should be an Object, but %v given.',
- relName,
- modelName,
- relDef,
- );
- if (!relDef.type || !Object.values(Type).includes(relDef.type))
- throw new InvalidArgumentError(
- 'The relation %v of the model %v requires the option "type" ' +
- 'to have one of relation types: %l, but %v given.',
- relName,
- modelName,
- Object.values(Type),
- relDef.type,
- );
- this._validateBelongsTo(modelName, relName, relDef);
- this._validateHasOne(modelName, relName, relDef);
- this._validateHasMany(modelName, relName, relDef);
- this._validateReferencesMany(modelName, relName, relDef);
- }
- /**
- * Validate "belongsTo".
- *
- * @example The regular "belongsTo" relation.
- * ```
- * {
- * type: RelationType.BELONGS_TO,
- * model: 'model',
- * foreignKey: 'modelId', // optional
- * }
- * ```
- *
- * @example The polymorphic "belongsTo" relation.
- * ```
- * {
- * type: RelationType.BELONGS_TO,
- * polymorphic: true,
- * foreignKey: 'referenceId', // optional
- * discriminator: 'referenceType, // optional
- * }
- * ```
- *
- * @param {string} modelName
- * @param {string} relName
- * @param {object} relDef
- * @private
- */
- _validateBelongsTo(modelName, relName, relDef) {
- if (relDef.type !== Type.BELONGS_TO) return;
- if (relDef.polymorphic) {
- // A polymorphic "belongsTo" relation.
- if (typeof relDef.polymorphic !== 'boolean')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "belongsTo", ' +
- 'so it expects the option "polymorphic" to be a Boolean, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.polymorphic,
- );
- if (relDef.foreignKey && typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v is a polymorphic "belongsTo" relation, ' +
- 'so it expects the provided option "foreignKey" to be a String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (relDef.discriminator && typeof relDef.discriminator !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v is a polymorphic "belongsTo" relation, ' +
- 'so it expects the provided option "discriminator" to be a String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.discriminator,
- );
- } else {
- // A regular "belongsTo" relation.
- if (!relDef.model || typeof relDef.model !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "belongsTo", ' +
- 'so it requires the option "model" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.model,
- );
- if (relDef.foreignKey && typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "belongsTo", ' +
- 'so it expects the provided option "foreignKey" to be a String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v is a non-polymorphic "belongsTo" relation, ' +
- 'so it should not have the option "discriminator" to be provided.',
- relName,
- modelName,
- );
- }
- }
- /**
- * Validate "hasOne".
- *
- * @example The regular "hasOne" relation.
- * ```
- * {
- * type: RelationType.HAS_ONE,
- * model: 'model',
- * foreignKey: 'modelId',
- * }
- * ```
- *
- * @example The polymorphic "hasOne" relation with a target relation name.
- * ```
- * {
- * type: RelationType.HAS_ONE,
- * model: 'model',
- * polymorphic: 'reference',
- * }
- * ```
- *
- * @example The polymorphic "hasOne" relation with target relation keys.
- * ```
- * {
- * type: RelationType.HAS_ONE,
- * model: 'model',
- * polymorphic: true,
- * foreignKey: 'referenceId',
- * discriminator: 'referenceType,
- * }
- * ```
- *
- * @param {string} modelName
- * @param {string} relName
- * @param {object} relDef
- * @private
- */
- _validateHasOne(modelName, relName, relDef) {
- if (relDef.type !== RelationType.HAS_ONE) return;
- if (!relDef.model || typeof relDef.model !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasOne", ' +
- 'so it requires the option "model" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.model,
- );
- if (relDef.polymorphic) {
- if (typeof relDef.polymorphic === 'string') {
- // A polymorphic "hasOne" relation with a target relation name.
- if (relDef.foreignKey)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" with ' +
- 'a String value, so it should not have the option "foreignKey" ' +
- 'to be provided.',
- relName,
- modelName,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" with ' +
- 'a String value, so it should not have the option "discriminator" ' +
- 'to be provided.',
- relName,
- modelName,
- );
- } else if (typeof relDef.polymorphic === 'boolean') {
- // A polymorphic "hasOne" relation with target relation keys.
- if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" ' +
- 'with "true" value, so it requires the option "foreignKey" ' +
- 'to be a non-empty String, but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (!relDef.discriminator || typeof relDef.discriminator !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" ' +
- 'with "true" value, so it requires the option "discriminator" ' +
- 'to be a non-empty String, but %v given.',
- relName,
- modelName,
- relDef.discriminator,
- );
- } else {
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasOne", ' +
- 'so it expects the provided option "polymorphic" to be ' +
- 'a String or a Boolean, but %v given.',
- relName,
- modelName,
- relDef.polymorphic,
- );
- }
- } else {
- // A regular "hasOne" relation.
- if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasOne", ' +
- 'so it requires the option "foreignKey" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v is a non-polymorphic "hasOne" relation, ' +
- 'so it should not have the option "discriminator" to be provided.',
- relName,
- modelName,
- );
- }
- }
- /**
- * Validate "hasMany".
- *
- * @example The regular "hasMany" relation.
- * ```
- * {
- * type: RelationType.HAS_MANY,
- * model: 'model',
- * foreignKey: 'modelId',
- * }
- * ```
- *
- * @example The polymorphic "hasMany" relation with a target relation name.
- * ```
- * {
- * type: RelationType.HAS_MANY,
- * model: 'model',
- * polymorphic: 'reference',
- * }
- * ```
- *
- * @example The polymorphic "hasMany" relation with target relation keys.
- * ```
- * {
- * type: RelationType.HAS_MANY,
- * model: 'model',
- * polymorphic: true,
- * foreignKey: 'referenceId',
- * discriminator: 'referenceType,
- * }
- * ```
- *
- * @param {string} modelName
- * @param {string} relName
- * @param {object} relDef
- * @private
- */
- _validateHasMany(modelName, relName, relDef) {
- if (relDef.type !== RelationType.HAS_MANY) return;
- if (!relDef.model || typeof relDef.model !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasMany", ' +
- 'so it requires the option "model" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.model,
- );
- if (relDef.polymorphic) {
- if (typeof relDef.polymorphic === 'string') {
- // A polymorphic "hasMany" relation with a target relation name.
- if (relDef.foreignKey)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" with ' +
- 'a String value, so it should not have the option "foreignKey" ' +
- 'to be provided.',
- relName,
- modelName,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" with ' +
- 'a String value, so it should not have the option "discriminator" ' +
- 'to be provided.',
- relName,
- modelName,
- );
- } else if (typeof relDef.polymorphic === 'boolean') {
- // A polymorphic "hasMany" relation with target relation keys.
- if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" ' +
- 'with "true" value, so it requires the option "foreignKey" ' +
- 'to be a non-empty String, but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (!relDef.discriminator || typeof relDef.discriminator !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the option "polymorphic" ' +
- 'with "true" value, so it requires the option "discriminator" ' +
- 'to be a non-empty String, but %v given.',
- relName,
- modelName,
- relDef.discriminator,
- );
- } else {
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasMany", ' +
- 'so it expects the provided option "polymorphic" to be ' +
- 'a String or a Boolean, but %v given.',
- relName,
- modelName,
- relDef.polymorphic,
- );
- }
- } else {
- // A regular "hasMany" relation.
- if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "hasMany", ' +
- 'so it requires the option "foreignKey" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v is a non-polymorphic "hasMany" relation, ' +
- 'so it should not have the option "discriminator" to be provided.',
- relName,
- modelName,
- );
- }
- }
- /**
- * Validate "referencesMany".
- *
- * @example
- * ```
- * {
- * type: RelationType.REFERENCES_MANY,
- * model: 'model',
- * foreignKey: 'modelIds', // optional
- * }
- * ```
- *
- * @param {string} modelName
- * @param {string} relName
- * @param {object} relDef
- * @private
- */
- _validateReferencesMany(modelName, relName, relDef) {
- if (relDef.type !== Type.REFERENCES_MANY) return;
- if (!relDef.model || typeof relDef.model !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "referencesMany", ' +
- 'so it requires the option "model" to be a non-empty String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.model,
- );
- if (relDef.foreignKey && typeof relDef.foreignKey !== 'string')
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "referencesMany", ' +
- 'so it expects the provided option "foreignKey" to be a String, ' +
- 'but %v given.',
- relName,
- modelName,
- relDef.foreignKey,
- );
- if (relDef.discriminator)
- throw new InvalidArgumentError(
- 'The relation %v of the model %v has the type "referencesMany", ' +
- 'so it should not have the option "discriminator" to be provided.',
- relName,
- modelName,
- );
- }
- }
|