|
| 1 | +/** |
| 2 | + * The instance‐side of an Assert (no static factory methods). |
| 3 | + * All core “is.X()” methods and custom asserts produce this. |
| 4 | + */ |
| 5 | +interface AssertInstance { |
| 6 | + /** Returns `true` if the value passes, otherwise returns a Violation/object. */ |
| 7 | + check(value: unknown, group?: string | string[], context?: unknown): true | any; |
| 8 | + /** Throws on failure; returns `true` if the value passes. */ |
| 9 | + validate(value: unknown, group?: string | string[], context?: unknown): true; |
| 10 | + /** Does this assert apply to the given validation group(s)? */ |
| 11 | + requiresValidation(group?: string | string[]): boolean; |
| 12 | + /** Is this assert assigned to the given group? */ |
| 13 | + hasGroup(group: string | string[]): boolean; |
| 14 | + /** Is this assert in any of the provided groups? */ |
| 15 | + hasOneOf(groups: string[]): boolean; |
| 16 | + /** Does this assert belong to at least one group? */ |
| 17 | + hasGroups(): boolean; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Core `validator.js-asserts` methods (lower-cased). |
| 22 | + */ |
| 23 | +export interface ValidatorJSAsserts { |
| 24 | + /** |
| 25 | + * Valid ABA (American Bankers Association) Routing Number used in ACH payments. |
| 26 | + * @requires abavalidator |
| 27 | + */ |
| 28 | + abaRoutingNumber(): AssertInstance; |
| 29 | + |
| 30 | + /** Valid BIC (Bank Identifier Code) used for international wire transfers. */ |
| 31 | + bankIdentifierCode(): AssertInstance; |
| 32 | + |
| 33 | + /** Valid `BigNumber`. @requires bignumber.js */ |
| 34 | + bigNumber(options?: { validateSignificantDigits?: boolean }): AssertInstance; |
| 35 | + |
| 36 | + /** `BigNumber` equal to the given value. @requires bignumber.js */ |
| 37 | + bigNumberEqualTo(value: string | number, options?: { validateSignificantDigits?: boolean }): AssertInstance; |
| 38 | + |
| 39 | + /** `BigNumber` > threshold. @requires bignumber.js */ |
| 40 | + bigNumberGreaterThan(threshold: string | number, options?: { validateSignificantDigits?: boolean }): AssertInstance; |
| 41 | + |
| 42 | + /** `BigNumber` ≥ threshold. @requires bignumber.js */ |
| 43 | + bigNumberGreaterThanOrEqualTo( |
| 44 | + threshold: string | number, |
| 45 | + options?: { validateSignificantDigits?: boolean } |
| 46 | + ): AssertInstance; |
| 47 | + |
| 48 | + /** `BigNumber` < threshold. @requires bignumber.js */ |
| 49 | + bigNumberLessThan(threshold: string | number, options?: { validateSignificantDigits?: boolean }): AssertInstance; |
| 50 | + |
| 51 | + /** `BigNumber` ≤ threshold. @requires bignumber.js */ |
| 52 | + bigNumberLessThanOrEqualTo( |
| 53 | + threshold: string | number, |
| 54 | + options?: { validateSignificantDigits?: boolean } |
| 55 | + ): AssertInstance; |
| 56 | + |
| 57 | + /** Value is a boolean. */ |
| 58 | + boolean(): AssertInstance; |
| 59 | + |
| 60 | + /** Valid Canadian ZIP code (postal code). */ |
| 61 | + caZipCode(): AssertInstance; |
| 62 | + |
| 63 | + /** Run a custom callback function, passing a custom class name. */ |
| 64 | + callback(fn: (value: unknown) => boolean, customClass: string): AssertInstance; |
| 65 | + |
| 66 | + /** Valid Brazilian CPF number. @requires cpf */ |
| 67 | + cpfNumber(): AssertInstance; |
| 68 | + |
| 69 | + /** Valid credit card number. @requires creditcard */ |
| 70 | + creditCard(): AssertInstance; |
| 71 | + |
| 72 | + /** |
| 73 | + * Valid Mexican CURP number. |
| 74 | + * @requires curp |
| 75 | + */ |
| 76 | + curpNumber(): AssertInstance; |
| 77 | + |
| 78 | + /** |
| 79 | + * Valid `moment` date, (optionally with format). |
| 80 | + * - If `moment` is not available, returns `true` by default. |
| 81 | + * @requires moment |
| 82 | + */ |
| 83 | + date(options?: { format?: string }): AssertInstance; |
| 84 | + |
| 85 | + /** Date difference > threshold. @requires moment */ |
| 86 | + dateDiffGreaterThan( |
| 87 | + threshold: number, |
| 88 | + options?: { |
| 89 | + absolute?: boolean; |
| 90 | + asFloat?: boolean; |
| 91 | + fromDate?: Date | string | null; |
| 92 | + unit?: string; |
| 93 | + } |
| 94 | + ): AssertInstance; |
| 95 | + |
| 96 | + /** Date difference ≥ threshold. @requires moment */ |
| 97 | + dateDiffGreaterThanOrEqualTo( |
| 98 | + threshold: number, |
| 99 | + options?: { |
| 100 | + absolute?: boolean; |
| 101 | + asFloat?: boolean; |
| 102 | + fromDate?: Date | string | null; |
| 103 | + unit?: string; |
| 104 | + } |
| 105 | + ): AssertInstance; |
| 106 | + |
| 107 | + /** Date difference < threshold. @requires moment */ |
| 108 | + dateDiffLessThan( |
| 109 | + threshold: number, |
| 110 | + options?: { |
| 111 | + absolute?: boolean; |
| 112 | + asFloat?: boolean; |
| 113 | + fromDate?: Date | string | null; |
| 114 | + unit?: string; |
| 115 | + } |
| 116 | + ): AssertInstance; |
| 117 | + |
| 118 | + /** Date difference ≤ threshold. @requires moment */ |
| 119 | + dateDiffLessThanOrEqualTo( |
| 120 | + threshold: number, |
| 121 | + options?: { |
| 122 | + absolute?: boolean; |
| 123 | + asFloat?: boolean; |
| 124 | + fromDate?: Date | string | null; |
| 125 | + unit?: string; |
| 126 | + } |
| 127 | + ): AssertInstance; |
| 128 | + |
| 129 | + /** |
| 130 | + * Extends base email address assert. |
| 131 | + * - Max length is 254 characters. |
| 132 | + * - Returns `true` if email ends in `.deleted`. |
| 133 | + * @requires validator |
| 134 | + */ |
| 135 | + email(): AssertInstance; |
| 136 | + |
| 137 | + /** Object has exactly the specified keys. */ |
| 138 | + equalKeys(...keys: string[] | [string[]]): AssertInstance; |
| 139 | + |
| 140 | + /** Validate a hash string using a specific algorithm. */ |
| 141 | + hash(algorithm: 'sha1' | 'sha256' | 'sha512'): AssertInstance; |
| 142 | + |
| 143 | + /** Valid integer. */ |
| 144 | + integer(): AssertInstance; |
| 145 | + |
| 146 | + /** Valid IBAN (International Bank Account Number). @requires iban */ |
| 147 | + internationalBankAccountNumber(): AssertInstance; |
| 148 | + |
| 149 | + /** Value is an IP address. */ |
| 150 | + ip(): AssertInstance; |
| 151 | + |
| 152 | + /** Valid ISO 3166 country code. @requires isoc */ |
| 153 | + iso3166Country(): AssertInstance; |
| 154 | + |
| 155 | + /** Value is a JSON string. */ |
| 156 | + json(): AssertInstance; |
| 157 | + |
| 158 | + /** Value is not empty. */ |
| 159 | + notEmpty(): AssertInstance; |
| 160 | + |
| 161 | + /** Value is null or passes the provided assert. */ |
| 162 | + nullOr(assert: AssertInstance): AssertInstance; |
| 163 | + |
| 164 | + /** Value is null or a boolean. */ |
| 165 | + nullOrBoolean(): AssertInstance; |
| 166 | + |
| 167 | + /** Value is null or a date. */ |
| 168 | + nullOrDate(): AssertInstance; |
| 169 | + |
| 170 | + /** Value is null or a string (length within `[min, max]`). */ |
| 171 | + nullOrString(boundaries?: { min?: number; max?: number }): AssertInstance; |
| 172 | + |
| 173 | + /** Valid phone number (optionally by country code). @requires google-libphonenumber */ |
| 174 | + phone(options?: { countryCode?: string }): AssertInstance; |
| 175 | + |
| 176 | + /** Value is a plain object. */ |
| 177 | + plainObject(): AssertInstance; |
| 178 | + |
| 179 | + /** Valid Mexican RFC number. @requires validate-rfc */ |
| 180 | + rfcNumber(): AssertInstance; |
| 181 | + |
| 182 | + /** Valid TIN (Taxpayer Identification Number). @requires tin-validator */ |
| 183 | + taxpayerIdentificationNumber(): AssertInstance; |
| 184 | + |
| 185 | + /** Valid UK bank account with modulus checking. @requires uk-modulus-checking */ |
| 186 | + ukModulusChecking(): AssertInstance; |
| 187 | + |
| 188 | + /** Valid `URI`. @requires uri-js */ |
| 189 | + uri(constraints?: Record<string, any>): AssertInstance; |
| 190 | + |
| 191 | + /** Valid US subdivision code (state). */ |
| 192 | + usSubdivision(options?: { categories?: string[]; alpha2Only?: boolean }): AssertInstance; |
| 193 | + |
| 194 | + /** Valid US ZIP code. */ |
| 195 | + usZipCode(): AssertInstance; |
| 196 | + |
| 197 | + /** Valid `UUID` (version 3, 4, or 5). */ |
| 198 | + uuid(version?: 3 | 4 | 5): AssertInstance; |
| 199 | +} |
0 commit comments