function validateNode()

in packages/utils/blueprint-cli/src/validate-options.ts [63:119]


function validateNode(node: Node, values: string[], regex: string): ValiationError[] {
  if (node.kind != SupportedKind.StringKeyword) {
    throw new Error(`regex validation is not supported on ${node.kind}`);
  }

  const error = {
    location: node.path,
    validationRegex: regex,
  };
  if (!regex) {
    const validationMessage = [
      `${SupportedKind.StringKeyword} at ${node.path} should have a @${VALIDATION_TAG} annotation.`,
      'Example: alpha numeric 5-50 character regex',
      `[@${VALIDATION_TAG} /^[a-zA-Z0-9]{1,50}$/]`,
      '[@validationMessage Must contain only alphanumeric characters and be up to 50 characters in length]',
    ].join(' - ');
    return [
      {
        ...error,
        level: 'WARNING',
        value: JSON.stringify(values),
        validationMessage,
      },
    ];
  }
  if (regex[0] == '/') {
    regex = regex.slice(1);
  }
  if (regex[regex.length - 1] == '/') {
    regex = regex.slice(0, -1);
  }

  if (!values) {
    return [
      {
        ...error,
        level: 'WARNING',
        value: '',
        validationMessage: `Could not find an element at ${node.path}`,
      },
    ];
  }

  const regexEx = new RegExp(regex);
  const errors: ValiationError[] = [];
  for (const value of values || []) {
    if (!regexEx.test(value)) {
      errors.push({
        ...error,
        value: value,
        level: 'ERROR',
        validationMessage: node.jsDoc?.tags![`${VALIDATION_MESSAGE_TAG}`] || 'Input validation error',
      });
    }
  }
  return errors;
}