Skip to content

Commit

Permalink
refactor: update build process to enable tree shaking (#568)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 

Validation functions was removed from `Validator` class to enable tree shaking.

BEFORE:

    import {Validator} from "class-validator";

    const validator = new Validator();
    validator.isNotIn(value, possibleValues);
    validator.isBoolean(value);

AFTER:

    import {isNotIn, isBoolean} from "class-validator";

    isNotIn(value, possibleValues);
    isBoolean(value);

Closes #258, #248, #247, #212
  • Loading branch information
vlapo committed Apr 17, 2020
1 parent 7294108 commit 11a7b8b
Show file tree
Hide file tree
Showing 105 changed files with 3,973 additions and 2,981 deletions.
50 changes: 46 additions & 4 deletions .gitignore
@@ -1,6 +1,48 @@
build/
node_modules/
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Yarn Integrity file
.yarn-integrity

# Output of 'npm pack'
*.tgz

# Optional npm cache directory
.npm

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Coverage report dir
coverage/
npm-debug.log

# Dependency directories
node_modules/

# rollup.js build output
dist/
build/


# IDEs
*.iml
.idea/
.idea/
.vscode/

105 changes: 5 additions & 100 deletions README.md
Expand Up @@ -790,105 +790,10 @@ If you want to perform a simple non async validation you can use `validateSync`
There are several method exist in the Validator that allows to perform non-decorator based validation:

```typescript
import {Validator} from "class-validator";

// Validation methods
const validator = new Validator();

// common validation methods
validator.isDefined(value); // Checks if value is defined ("!==undefined").
validator.equals(value, comparison); // Checks if value matches ("===") the comparison.
validator.notEquals(value, comparison); // Checks if value does not match ("!==") the comparison.
validator.isEmpty(value); // Checks if given value is empty (=== '', === null, === undefined).
validator.isNotEmpty(value); // Checks if given value is not empty (!== '', !== null, !== undefined).
validator.isIn(value, possibleValues); // Checks if given value is in a array of allowed values.
validator.isNotIn(value, possibleValues); // Checks if given value not in a array of allowed values.

// type validation methods
validator.isBoolean(value); // Checks if a given value is a real boolean.
validator.isDate(value); // Checks if a given value is a real date.
validator.isString(value); // Checks if a given value is a real string.
validator.isArray(value); // Checks if a given value is an array.
validator.isNumber(value, options); // Checks if a given value is a real number.
validator.isInt(value); // Checks if value is an integer.
validator.isEnum(value, entity); // Checks if value is valid for a certain enum entity.

// number validation methods
validator.isDivisibleBy(value, num); // Checks if value is a number that's divisible by another.
validator.isPositive(value); // Checks if the value is a positive number.
validator.isNegative(value); // Checks if the value is a negative number.
validator.min(num, min); // Checks if the first number is greater than or equal to the second.
validator.max(num, max); // Checks if the first number is less than or equal to the second.

// date validation methods
validator.minDate(date, minDate); // Checks if the value is a date that's after the specified date.
validator.maxDate(date, maxDate); // Checks if the value is a date that's before the specified date.

// string-type validation methods
validator.isBooleanString(str); // Checks if a string is a boolean.
validator.isNumberString(str); // Checks if the string is numeric.

// string validation methods
validator.contains(str, seed); // Checks if the string contains the seed.
validator.notContains(str, seed); // Checks if the string does not contain the seed.
validator.isAlpha(str); // Checks if the string contains only letters (a-zA-Z).
validator.isAlphanumeric(str); // Checks if the string contains only letters and numbers.
validator.isDecimal(str, options); // Checks if the string is a valid decimal value.
validator.isAscii(str); // Checks if the string contains ASCII chars only.
validator.isBase64(str); // Checks if a string is base64 encoded.
validator.isByteLength(str, min, max); // Checks if the string's length (in bytes) falls in a range.
validator.isCreditCard(str); // Checks if the string is a credit card.
validator.isCurrency(str, options); // Checks if the string is a valid currency amount.
validator.isEmail(str, options); // Checks if the string is an email.
validator.isFQDN(str, options); // Checks if the string is a fully qualified domain name (e.g. domain.com).
validator.isFullWidth(str); // Checks if the string contains any full-width chars.
validator.isHalfWidth(str); // Checks if the string contains any half-width chars.
validator.isVariableWidth(str); // Checks if the string contains variable-width chars.
validator.isHexColor(str); // Checks if the string is a hexadecimal color.
validator.isHexadecimal(str); // Checks if the string is a hexadecimal number.
validator.isMACAddress(str); // Checks if the string is a MAC Address.
validator.isIP(str, version); // Checks if the string is an IP (version 4 or 6).
validator.isPort(str); // Check if the string is a valid port number.
validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 or 13).
validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier).
validator.isISO8601(str, options); // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse).
validator.isJWT(str) // Checks if the string is valid JWT.
validator.isObject(object); // Checks if the object is valid Object (null, functions, arrays will return false)
validator.isNotEmptyObject(object); // Checks if the object is not empty
validator.isLowercase(str); // Checks if the string is lowercase.
validator.isLatLong(str); // Checks if the string is a valid latitude-longitude coordinate in the format lat,long
validator.isLatitude(str); // Checks if the string or number is a valid latitude coordinate
validator.isLongtitude(str); // Checks if the string or number is a valid longitude coordinate
validator.isMobilePhone(str, locale); // Checks if the string is a mobile phone number.
validator.isISO31661Alpha2(str); // Check if the string is a valid ISO 3166-1 alpha-2
validator.isISO31661Alpha3(str); // Check if the string is a valid ISO 3166-1 alpha-3
validator.isPhoneNumber(str, region); // Checks if the string is a valid phone number.
validator.isMongoId(str); // Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
validator.isMultibyte(str); // Checks if the string contains one or more multibyte chars.
validator.isSurrogatePair(str); // Checks if the string contains any surrogate pairs chars.
validator.isURL(str, options); // Checks if the string is an url.
validator.isUUID(str, version); // Checks if the string is a UUID (version 3, 4, 5 or all).
validator.IsFirebasePushId(str); // Checks if the string is a Firebase Push Id
validator.isUppercase(str); // Checks if the string is uppercase.
validator.length(str, min, max); // Checks if the string's length falls in a range.
validator.minLength(str, min); // Checks if the string's length is not less than given number.
validator.maxLength(str, max); // Checks if the string's length is not more than given number.
validator.matches(str, pattern, modifiers); // Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
validator.isMilitaryTime(str); // Checks if the string is a valid representation of military time in the format HH:MM.
validator.isHash(str, algorithm); // Checks if the string is a hash of type algorithm.
validator.isISSN(str, options); // Checks if the string is a ISSN.

// array validation methods
validator.arrayContains(array, values); // Checks if array contains all values from the given array of values.
validator.arrayNotContains(array, values); // Checks if array does not contain any of the given values.
validator.arrayNotEmpty(array); // Checks if given array is not empty.
validator.arrayMinSize(array, min); // Checks if array's length is at least `min` number.
validator.arrayMaxSize(array, max); // Checks if array's length is as most `max` number.
validator.arrayUnique(array); // Checks if all array's values are unique. Comparison for objects is reference-based.

// object validation methods
validator.isInstance(value, target); // Checks value is an instance of the target.
import {isEmpty, isBoolean} from "class-validator";

isEmpty(value);
isBoolean(value);
```

## Validation decorators
Expand Down Expand Up @@ -943,7 +848,7 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
| `@IsVariableWidth()` | Checks if the string contains a mixture of full and half-width chars. |
| `@IsHexColor()` | Checks if the string is a hexadecimal color. |
| `@IsHexadecimal()` | Checks if the string is a hexadecimal number. |
| `@IsMACAddress()` | Checks if the string is a MAC Address. |
| `@IsMACAddress(options?: IsMACAddressOptions)` | Checks if the string is a MAC Address. |
| `@IsIP(version?: "4"\|"6")` | Checks if the string is an IP (version 4 or 6). |
| `@IsPort()` | Check if the string is a valid port number. |
| `@IsISBN(version?: "10"\|"13")` | Checks if the string is an ISBN (version 10 or 13). |
Expand Down

0 comments on commit 11a7b8b

Please sign in to comment.