index.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /// <reference types="node" />
  2. import * as Hoek from '@hapi/hoek';
  3. export namespace domain {
  4. /**
  5. * Analyzes a string to verify it is a valid domain name.
  6. *
  7. * @param domain - the domain name to validate.
  8. * @param options - optional settings.
  9. *
  10. * @return - undefined when valid, otherwise an object with single error key with a string message value.
  11. */
  12. function analyze(domain: string, options?: Options): Analysis | null;
  13. /**
  14. * Analyzes a string to verify it is a valid domain name.
  15. *
  16. * @param domain - the domain name to validate.
  17. * @param options - optional settings.
  18. *
  19. * @return - true when valid, otherwise false.
  20. */
  21. function isValid(domain: string, options?: Options): boolean;
  22. interface Options {
  23. /**
  24. * Determines whether Unicode characters are allowed.
  25. *
  26. * @default true
  27. */
  28. readonly allowUnicode?: boolean;
  29. /**
  30. * The minimum number of domain segments (e.g. `x.y.z` has 3 segments) required.
  31. *
  32. * @default 2
  33. */
  34. readonly minDomainSegments?: number;
  35. /**
  36. * Top-level-domain options
  37. *
  38. * @default true
  39. */
  40. readonly tlds?: Tlds.Allow | Tlds.Deny | boolean;
  41. }
  42. namespace Tlds {
  43. interface Allow {
  44. readonly allow: Set<string> | true;
  45. }
  46. interface Deny {
  47. readonly deny: Set<string>;
  48. }
  49. }
  50. }
  51. export namespace email {
  52. /**
  53. * Analyzes a string to verify it is a valid email address.
  54. *
  55. * @param email - the email address to validate.
  56. * @param options - optional settings.
  57. *
  58. * @return - undefined when valid, otherwise an object with single error key with a string message value.
  59. */
  60. function analyze(email: string, options?: Options): Analysis | null;
  61. /**
  62. * Analyzes a string to verify it is a valid email address.
  63. *
  64. * @param email - the email address to validate.
  65. * @param options - optional settings.
  66. *
  67. * @return - true when valid, otherwise false.
  68. */
  69. function isValid(email: string, options?: Options): boolean;
  70. interface Options extends domain.Options {
  71. /**
  72. * Determines whether to ignore the standards maximum email length limit.
  73. *
  74. * @default false
  75. */
  76. readonly ignoreLength?: boolean;
  77. }
  78. }
  79. export interface Analysis {
  80. /**
  81. * The reason validation failed.
  82. */
  83. error: string;
  84. /**
  85. * The error code.
  86. */
  87. code: string;
  88. }
  89. export const errors: Record<string, string>;
  90. export namespace ip {
  91. /**
  92. * Generates a regular expression used to validate IP addresses.
  93. *
  94. * @param options - optional settings.
  95. *
  96. * @returns an object with the regular expression and meta data.
  97. */
  98. function regex(options?: Options): Expression;
  99. interface Options {
  100. /**
  101. * The required CIDR mode.
  102. *
  103. * @default 'optional'
  104. */
  105. readonly cidr?: Cidr;
  106. /**
  107. * The allowed versions.
  108. *
  109. * @default ['ipv4', 'ipv6', 'ipvfuture']
  110. */
  111. readonly version?: Version | Version[];
  112. }
  113. type Cidr = 'optional' | 'required' | 'forbidden';
  114. type Version = 'ipv4' | 'ipv6' | 'ipvfuture';
  115. interface Expression {
  116. /**
  117. * The CIDR mode.
  118. */
  119. cidr: Cidr;
  120. /**
  121. * The raw regular expression string.
  122. */
  123. raw: string;
  124. /**
  125. * The regular expression.
  126. */
  127. regex: RegExp;
  128. /**
  129. * The array of versions allowed.
  130. */
  131. versions: Version[];
  132. }
  133. }
  134. export namespace uri {
  135. /**
  136. * Faster version of decodeURIComponent() that does not throw.
  137. *
  138. * @param string - the URL string to decode.
  139. *
  140. * @returns the decoded string or null if invalid.
  141. */
  142. function decode(string: string): string | null;
  143. /**
  144. * Generates a regular expression used to validate URI addresses.
  145. *
  146. * @param options - optional settings.
  147. *
  148. * @returns an object with the regular expression and meta data.
  149. */
  150. function regex(options?: Options): Expression;
  151. type Options = Hoek.ts.XOR<Options.Options, Options.Relative>;
  152. namespace Options {
  153. interface Query {
  154. /**
  155. * Allow the use of [] in query parameters.
  156. *
  157. * @default false
  158. */
  159. readonly allowQuerySquareBrackets?: boolean;
  160. }
  161. interface Relative extends Query {
  162. /**
  163. * Requires the URI to be relative.
  164. *
  165. * @default false
  166. */
  167. readonly relativeOnly?: boolean;
  168. }
  169. interface Options extends Query {
  170. /**
  171. * Allow relative URIs.
  172. *
  173. * @default false
  174. */
  175. readonly allowRelative?: boolean;
  176. /**
  177. * Capture domain segment ($1).
  178. *
  179. * @default false
  180. */
  181. readonly domain?: boolean;
  182. /**
  183. * The allowed URI schemes.
  184. */
  185. readonly scheme?: Scheme | Scheme[];
  186. }
  187. type Scheme = string | RegExp;
  188. }
  189. interface Expression {
  190. /**
  191. * The raw regular expression string.
  192. */
  193. raw: string;
  194. /**
  195. * The regular expression.
  196. */
  197. regex: RegExp;
  198. }
  199. }