All files / services/module/helpers validators.ts

12.12% Statements 4/33
0% Branches 0/16
0% Functions 0/8
6.45% Lines 2/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x       1x                                                                                                                                                                  
import { Service } from '../../../container';
import { Metadata, DecoratorType } from '../../../decorators/module/module.interfaces';
 
@Service()
export class ModuleValidators {
 
    validateEmpty(m, original: { metadata: Metadata }, type: DecoratorType) {
        if (!m) {
            const requiredType = type.charAt(0).toUpperCase() + type.slice(1);
            throw new Error(`
            ${original.metadata.raw}
            -> @Module: ${original.metadata.moduleName}
            -> @Module hash: ${original.metadata.moduleHash}
                --> Maybe you forgot to import some ${requiredType} inside ${original.metadata.moduleName} ?
 
                Hint: run ts-lint again, looks like imported ${requiredType} is undefined or null inside ${original.metadata.moduleName}
            `);
        }
    }
 
    genericWrongPluggableError(m, original: { metadata: Metadata }, type: DecoratorType) {
        if (m.metadata.type !== type) {
            const moduleType = m.metadata.type.charAt(0).toUpperCase() + m.metadata.type.slice(1);
            const requiredType = type.charAt(0).toUpperCase() + type.slice(1);
            throw new Error(`
            ${original.metadata.raw}
            -> @Module: '${original.metadata.moduleName}'
            -> @Module hash: '${original.metadata.moduleHash}'
                --> @${moduleType} '${m.metadata.moduleName}' provided, where expected class decorated with '@${requiredType}' instead,
            -> @Hint: please provide class with @Service decorator or remove ${m.metadata.moduleName} class
            `);
        }
    }
 
    validateImports(m, original: { metadata: Metadata }) {
        if (m.metadata.type !== 'module') {
            throw new Error(`
            ${original.metadata.raw}
            -> @Module: '${original.metadata.moduleName}'
            -> @Module hash: '${original.metadata.moduleHash}'
                --> @${m.metadata.type.charAt(0).toUpperCase() + m.metadata.type.slice(1)} '${m.originalName}' provided, where expected class decorated with '@Module' instead,
            -> @Hint: please provide class with @Module decorator or remove ${m.originalName} from imports
            `);
        }
    }
 
    validateServices(m, original: { metadata: Metadata }) {
        this.validateEmpty(m, original, 'service');
        if (m.provide) {
            return;
        }
        this.genericWrongPluggableError(m, original, 'service');
    }
 
    validatePlugin(m, original: { metadata: Metadata }) {
        this.validateEmpty(m, original, 'plugin');
        if (m.provide) {
            return;
        }
        this.genericWrongPluggableError(m, original, 'plugin');
    }
 
    validateController(m, original: { metadata: Metadata }) {
        this.validateEmpty(m, original, 'controller');
        if (m.provide) {
            return;
        }
        this.genericWrongPluggableError(m, original, 'controller');
    }
 
    validateEffect(m, original: { metadata: Metadata }) {
        this.validateEmpty(m, original, 'effect');
        if (m.provide) {
            return;
        }
        this.genericWrongPluggableError(m, original, 'effect');
    }
 
    validateComponent(m, original: { metadata: Metadata }) {
        this.validateEmpty(m, original, 'component');
        if (m.provide) {
            return;
        }
        this.genericWrongPluggableError(m, original, 'component');
    }
}