All files / src/validation core-validator.ts

100% Statements 15/15
100% Branches 25/25
100% Functions 5/5
100% Lines 15/15

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            131x                                 16842x 3629x     13213x 16842x               13213x 13206x   7x 7x                     7x 7x                   13206x 13204x   2x                     1730x                
import { ValidationErrorItem } from '../errors/errors.ts';
import { SqlExpression } from '../expressions/methods.ts';
import type { AttributeDefinition } from '../model/model-definition.ts';
import type { AttributeValidateOptions } from './types.ts';
import type { ValidationTarget } from './validation-target.ts';
 
const STRING_TYPES: ReadonlySet<string> = new Set(['STRING', 'TEXT', 'CITEXT', 'CHAR']);
 
/** Checks the checks Sequelize v6 always runs: `allowNull` and string attributes holding objects. */
export class CoreValidator {
  /**
   * Validates one attribute value.
   *
   * @param target - Instance being validated.
   * @param attribute - Attribute definition.
   * @param value - Current value of the attribute.
   * @returns Failures; empty for auto-generated attributes and valid values.
   */
  items(
    target: ValidationTarget,
    attribute: AttributeDefinition,
    value: unknown,
  ): readonly ValidationErrorItem[] {
    if (attribute._autoGenerated === true || attribute.autoIncrement === true) {
      return [];
    }
    const item =
      this.#notNull(target, attribute, value) ?? this.#notString(target, attribute, value);
    return item === undefined ? [] : [item];
  }
 
  #notNull(
    target: ValidationTarget,
    attribute: AttributeDefinition,
    value: unknown,
  ): ValidationErrorItem | undefined {
    if (attribute.allowNull !== false || (value !== null && value !== undefined)) {
      return undefined;
    }
    const message = CoreValidator.#notNullMessage(target, attribute);
    return new ValidationErrorItem(
      message,
      'notNull Violation',
      attribute.fieldName,
      value,
      target.instance,
      'is_null',
    );
  }
 
  static #notNullMessage(target: ValidationTarget, attribute: AttributeDefinition): string {
    const notNull = (attribute.validate as AttributeValidateOptions | undefined)?.notNull;
    return typeof notNull === 'object' && notNull.msg !== undefined
      ? notNull.msg
      : `${target.definition.name}.${attribute.fieldName} cannot be null`;
  }
 
  #notString(
    target: ValidationTarget,
    attribute: AttributeDefinition,
    value: unknown,
  ): ValidationErrorItem | undefined {
    if (!STRING_TYPES.has(attribute.type.key) || !CoreValidator.#isStructured(value)) {
      return undefined;
    }
    return new ValidationErrorItem(
      `${attribute.fieldName} cannot be an array or an object`,
      'string violation',
      attribute.fieldName,
      value,
      target.instance,
      'not_a_string',
    );
  }
 
  static #isStructured(value: unknown): boolean {
    return (
      typeof value === 'object' &&
      value !== null &&
      !Buffer.isBuffer(value) &&
      !(value instanceof SqlExpression)
    );
  }
}