question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: support for validating property as string or string[]

See original GitHub issue

Hi, how could I validate a string or array of strings?

I have in my class:

@IsString()
public parameter: string | string[];

Is there a way to tell to class-validator that the parameter could be string or array?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
UrielChcommented, Nov 10, 2022

No Answer for a whole year…

Proper fix

The proper way would be to change interface ValidationOptions, replacing each?: boolean; by each?: boolean | "optional"; then add some changes in customValidations and change in ‘ValidationExecutor’ and update the error message generation logic.

Please let me know if you think I should try a PR.

Quick fix

For now, the easiest way is to write your validator:

File: IsStringOrStrings.ts

import { buildMessage, ValidateBy, ValidationOptions } from 'class-validator';

export const IS_STRING_OR_STRINGS = 'isStringOrStrings';

/**
 * Checks if value is an integer.
 */
export function isStringOrStrings(val: unknown): boolean {
  if (typeof val === 'string') return true;
  if (!Array.isArray(val)) return false;
  for (const v of val) {
    if (typeof v !== 'string') return false;
  }
  return true;
}

/**
 * Checks if value is an String or a Strings.
 * Do not set each: true, or except if your looking for a string[] or string[][]
 */
export function IsStringOrStrings(validationOptions?: ValidationOptions): PropertyDecorator {
  return ValidateBy(
    {
      name: IS_STRING_OR_STRINGS,
      validator: {
        validate: (value: unknown /*, args*/): boolean => isStringOrStrings(value),
        defaultMessage: buildMessage((eachPrefix) => eachPrefix + '$property must be a string of string array', validationOptions),
      },
    },
    validationOptions,
  );
}
0reactions
github-actions[bot]commented, Dec 15, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Validate String Properties in Business Objects
There are two more tricks that we wish to demonstrate about string validation. First one deals with putting a lower limit on string...
Read more >
Want to validate a string using property in C# [closed]
When validating within set accessor you should chech value , not backing field categoryName which should have already been valid:
Read more >
Custom Edit Validate | Support Center
Creating Edit validate rule and comparing 2 dates using java code. The 2 dates are properties in application.
Read more >
How To Use Schema Validation in MongoDB - DigitalOcean
Step 2 — Validating String Fields. In MongoDB, schema validation ... The next property in the validation document is the required field.
Read more >
Frequently Asked Questions - python-jsonschema
The JSON Schema specification does not deal with how to apply the patternProperties keyword to non-string properties. The behavior of this library is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found