private appendConstraintValidations()

in powershell/sdk/utility.ts [98:164]


  private appendConstraintValidations(valueReference: string, sb: Array<string>, model: Schema) {
    const schema = <any>model;
    if (schema.maximum !== undefined) {
      const rule = schema.exclusiveMaximum ? 'ExclusiveMaximum' : 'InclusiveMaximum';
      const cmp = schema.exclusiveMaximum ? '>=' : '>';
      sb.push(`if (${valueReference} ${cmp} ${schema.maximum})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.${rule}, "${valueReference.replace('this.', '')}", ${schema.maximum});`);
      sb.push('}');
    }
    if (schema.minimum !== undefined) {
      const rule = schema.exclusiveMinimum ? 'ExclusiveMinimum' : 'InclusiveMinimum';
      const cmp = schema.exclusiveMinimum ? '<=' : '<';
      sb.push(`if (${valueReference} ${cmp} ${schema.minimum})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.${rule}, "${valueReference.replace('this.', '')}", ${schema.minimum});`);
      sb.push('}');
    }
    if (schema.maxItems !== undefined) {
      sb.push(`if (${valueReference}.Count > ${schema.maxItems})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.MaxItems, "${valueReference.replace('this.', '')}", ${schema.maxItems});`);
      sb.push('}');
    }
    if (schema.maxLength !== undefined) {
      sb.push(`if (${valueReference}.Length > ${schema.maxLength})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.MaxLength, "${valueReference.replace('this.', '')}", ${schema.maxLength});`);
      sb.push('}');
    }
    if (schema.minLength !== undefined) {
      sb.push(`if (${valueReference}.Length < ${schema.minLength})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.MinLength, "${valueReference.replace('this.', '')}", ${schema.minLength});`);
      sb.push('}');
    }
    if (schema.minItems !== undefined) {
      sb.push(`if (${valueReference}.Count < ${schema.minItems})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.MinItems, "${valueReference.replace('this.', '')}", ${schema.minItems});`);
      sb.push('}');
    }
    if (schema.multipleOf !== undefined) {
      sb.push(`if (${valueReference} % ${schema.multipleOf} != 0)`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.MultipleOf, "${valueReference.replace('this.', '')}", ${schema.multipleOf});`);
      sb.push('}');
    }
    if (schema.pattern !== undefined) {
      // eslint-disable-next-line
      const constraintValue = "\"" + schema.pattern.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";
      let condition = `!System.Text.RegularExpressions.Regex.IsMatch(${valueReference}, ${constraintValue})`;
      if (schema.type === SchemaType.Dictionary) {
        condition = `!System.Linq.Enumerable.All(${valueReference}.Values, value => System.Text.RegularExpressions.Regex.IsMatch(value, ${constraintValue}))`;
      }
      sb.push(`if (${condition})`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.Pattern, "${valueReference.replace('this.', '')}", ${constraintValue});`);
      sb.push('}');
    }
    if (schema.uniqueItems !== undefined && 'true' === schema.uniqueItems.toString()) {
      sb.push(`if (${valueReference}.Count != System.Linq.Enumerable.Count(System.Linq.Enumerable.Distinct(${valueReference})))`);
      sb.push('{');
      sb.push(`    throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.UniqueItems, "${valueReference.replace('this.', '')}");`);
      sb.push('}');
    }
  }