{"id":22096540,"url":"https://github.com/pksilen/validated-types","last_synced_at":"2025-07-24T22:32:15.391Z","repository":{"id":57390314,"uuid":"404421243","full_name":"pksilen/validated-types","owner":"pksilen","description":"Library for creating validated Typescript types for integers, floats, string and arrays","archived":false,"fork":false,"pushed_at":"2023-08-11T07:22:33.000Z","size":982,"stargazers_count":18,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-18T03:02:54.922Z","etag":null,"topics":["node","type","typescript","validation"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pksilen.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-08T16:36:15.000Z","updated_at":"2023-03-20T16:03:04.000Z","dependencies_parsed_at":"2023-02-15T21:45:25.661Z","dependency_job_id":null,"html_url":"https://github.com/pksilen/validated-types","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pksilen%2Fvalidated-types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pksilen%2Fvalidated-types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pksilen%2Fvalidated-types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pksilen%2Fvalidated-types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pksilen","download_url":"https://codeload.github.com/pksilen/validated-types/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227482485,"owners_count":17779968,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node","type","typescript","validation"],"created_at":"2024-12-01T04:11:31.960Z","updated_at":"2024-12-01T04:11:32.769Z","avatar_url":"https://github.com/pksilen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validated-types\n\n`validated-types` is a library for Typescript to create types for validated integer, float, string and array values.\nIt also allows of creating semantic types and declare variables with semantic types. \n\n[![version][version-badge]][package]\n[![build][build]][circleci]\n[![coverage][coverage]][codecov]\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=pksilen_validated-types\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=pksilen_validated-types)\n[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pksilen_validated-types\u0026metric=bugs)](https://sonarcloud.io/dashboard?id=pksilen_validated-types)\n[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=pksilen_validated-types\u0026metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=pksilen_validated-types)\n[![Downloads][downloads]][package]\n[![MIT License][license-badge]][license]\n\n## Table of Contents\n\n- [Prerequisites](#prerequisites)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Introduction](#introduction)\n  - [Validate Integers](#validated-integers)\n  - [Validate Floats](#validated-floats)\n  - [Validate Strings](#validated-strings)\n  - [Validate Arrays](#validated-arrays)\n  - [Validate Objects](#validated-objects)\n  - [Create Semantic Types/Variables](#create-semantic-types)\n  - [Create Semantic Validated Integers](#create-semantic-validated-integers)\n  - [Create Semantic Validated Floats](#create-semantic-validated-floats)\n  - [Create Semantic Validated Strings](#create-semantic-validated-strings)\n- [API Documentation](#api-documentation)\n  - [VFloat](#vfloat)\n    - [registerCustomValidator](#vfloat-registerCustomValidator)\n    - [createOrThrow](#vfloat-createOrThrow)\n    - [create](#vfloat-create)\n    - [createOrError](#vfloat-createOrError)\n    - [value](#vfloat-value)\n  - [VInt](#vint)\n    - [registerCustomValidator](#vint-registerCustomValidator)\n    - [createOrThrow](#vint-createOrThrow)\n    - [create](#vint-create)\n    - [createOrError](#vint-createOrError)\n    - [value](#vint-value)\n  - [VString](#vstring)\n    - [registerCustomValidator](#vstring-registerCustomValidator)\n    - [createOrThrow](#vstring-createOrThrow)\n    - [create](#vstring-create)\n    - [createOrError](#vstring-createOrError)\n    - [value](#vstring-value)\n  - [VArray](#varray)\n    - [registerCustomValidator](#varray-registerCustomValidator)\n    - [createOrThrow](#varray-createOrThrow)\n    - [create](#varray-create)\n    - [createOrError](#varray-createOrError)\n    - [value](#varray-value)\n  - [SemType](#SemType)\n    - [constructor](#SemType-constructor)\n    - [value](#SemType-value)\n  - [SemVInt](#SemVInt)\n  - [SemVFloat](#SemVFloat)\n  - [SemVString](#SemVString)\n  - [VSpecOf](#vspecof)\n- [Feedback](#feedback)\n- [License](#license)\n\n## \u003ca name=\"prerequisites\"\u003e\u003c/a\u003e Prerequisites\n* Typescript \u003e= 4.1.0\n\n## \u003ca name=\"installation\"\u003e\u003c/a\u003e Installation\n\n```bash\nnpm install --save validated-types\n```\n\n## \u003ca name=\"usage\"\u003e\u003c/a\u003e Usage\n\n### \u003ca name=\"introduction\"\u003e\u003c/a\u003e Introduction\n\nValidated-types is a type value validation library for Typescript. You can validate values of integers, floats, strings and arrays.\nFor numbers, you can for example validate the minimum and maximum value. For strings, you can validate the length and also perform semantic validation\nto validate if a string should be, for example, a valid URL, IP address or email address.\n\nBy using validated types for the parameters of your application's functions, you can rest assured that only proper values are ever passed to your function.\nYou don't have to do any validation work inside your functions, just use the value of already validated parameter. The validation of the value is done only once upon the construction of validated objects,\nso there is no performance penalty in using the validated values as many times as you need.\n\nYou should create the validated integer, float, string and array objects in functions that receive unvalidated input data and then pass the validated values to the rest of the functions in your application.\n\nYour application typically receives unvalidated input data from external sources in following scenarios:\n\n- Reading command line arguments\n- Reading environment variables\n- Reading standard input\n- Reading file(s) from file system\n- Reading data from socket (network input)\n- End-user input from user interface\n\nYou can also create semantic types and semantically typed variables using `SemType`. Using semantic types, you\ncan differentiate between multiple types of the same basic type. For example, If a function accepts two boolean arguments, it is possible\nthat the calling function gives those two arguments in wrong order, but you never notice it, because it does not generate a compilation error.\nUsing semantic types, you can differentiate between those two boolean types by giving them two different semantic names. \nSemantic name is just any string describing the purpose of type/variable.\n\nThe library contains static factory methods in the classes forr creating a new instance of the specific class.\nThere are different factory methods available. You can choose which factory method to use depending on your\napplication need. The factory methods are following:\n\n- `tryCreate(...)` creates a validated value object or throws an exception\n- `createOrThrow(...)` is same as above, i.e. creates a validated value object or throws an exception\n- `create(...)` creates a validated value object or returns `null`. Use this method if you don't need the error reason\n- `createOrError(...)` creates a validated value object or returns an error object, always returns a pair (is similar to Golang returning an error)\n\n### \u003ca name=\"validated-integers\"\u003e\u003c/a\u003e Validate Integers\n\nYou can validate your integer, float and string type variables with `validated-types`.\nFor example, to create a validated integer which allows values between 1 and 10, you declare:\n\n```ts\nimport { VInt } from 'validated-types';\n\nfunction useInt(int: VInt\u003c'1,10'\u003e) {\n  // use int here\n  console.log(int.value);\n}\n\nconst int = VInt.tryCreate\u003c'1,10'\u003e('1,10', 5);\nconst maybeInt = VInt.create\u003c'1,10'\u003e('1,10', 12); // Returns null\n\nuseInt(int); // prints to console: 5\nuseInt(maybeInt ?? VInt.tryCreate('1,10', 10)); // prints to console: 10\n```\n\nYou can also register a custom validator:\n\n```ts\nimport { VInt } from 'validated-types';\n\nVInt.registerCustomValidator('isEven', (value) =\u003e value % 2 === 0);\n\nconst evenNumber = VInt.tryCreate\u003c'custom:isEven'\u003e('custom:isEven', 2);\nconst maybeEvenNumber = VInt\u003c'custom:isEven'\u003e.create('custom:isEven', 1);\n```\n\nCustom validator must be registered before it is used by any validated types `create`, `createOrThrow` or `createOrError`method.\nThis means that you should register your custom validators as early as possible in your application code.\nIf custom validator is not registered, and it is used, an exception will be thrown.\n\n### \u003ca name=\"validated-floats\"\u003e\u003c/a\u003e Validate Floats\n\nTo create a validated float which allows positive values only, you declare:\n\n```ts\nimport { VFloat } from 'validated-types';\n\nfunction useFloat(float: VFloat\u003c'positive'\u003e) {\n  // use float here\n  console.log(float.value);\n}\n\nconst float = VFloat.tryCreate\u003c'positive'\u003e('positive', 5.25);\nconst maybeFloat = VFloat\u003c'positive'\u003e.create('positive', -5.25); // returns null\n\nuseFloat(float); // prints to console: 5.25\nuseFloat(maybeFloat ?? VFloat.tryCreate('positive', 1)); // prints to console: 1\n```\n\n### \u003ca name=\"validated-strings\"\u003e\u003c/a\u003e Validate Strings\n\nTo create a validated string which allows URLs with minimum length of one and maximum length of 1024 characters, you declare:\n\n```ts\nimport { VString } from 'validated-types';\n\nfunction useUrl(url: VString\u003c'1,1024,url'\u003e) {\n  // use URL here\n  console.log(url.value);\n}\n\nconst url = VString.tryCreate\u003c'1,1024,url'\u003e('1,1024,url', 'https://www.mydomain.com');\nconst maybeUrl = VString\u003c'1,1024,url'\u003e.create('1,1024,url', 'invalid URL'); // Returns null\n\nuseUrl(url); // prints to console: https://www.mydomain.com\nuseUrl(maybeUrl ?? VString.tryCreate('1,1024,url', 'https://google.com')); // prints to console: https://google.com\n```\n\nYou can combine up to 5 different string validators together. Multiple validators are given as a tuple which can have 2-5 elements. The first element of the tuple should validate the length of the string, if needed. Rest of the elements in tuple should not validate the length of string anymore.\nBelow example contains 4 validators that validate following string:\n\n- is at least 1 characters long\n- is at most 1024 characters long\n- is lower case string\n- is valid URL\n- URL starts with _https_\n- URL ends with _.html_\n\n```ts\nimport { SpecOf, VString } from 'validated-types';\n\ntype Url = VString\u003c['1,1024,lowercase', 'url', 'startsWith,https', 'endsWith,.html']\u003e;\nconst urlVSpec: VSpecOf\u003cUrl\u003e = ['1,1024,lowercase', 'url', 'startsWith,https', 'endsWith,.html'];\n\nfunction useUrl(url: Url) {\n  // use URL here\n  console.log(url.value);\n}\n\nconst url: Url = VString.tryCreate(urlVSpec, 'https://server.domain.com:8080/index.html');\nconst maybeUrl: Url | null = VString.create(urlVSpec, 'invalid URL'); // Returns null\n\nuseUrl(url); // prints to console: https://server.domain.com:8080/index.html\nuseUrl(maybeUrl ?? VString.tryCreate(urlVSpec, 'https://server.domain.com:8080/index.html')); // prints to console: https://server.domain.com:8080/index.html\n```\n\n### \u003ca name=\"validated-arrays\"\u003e\u003c/a\u003e Validate Arrays\nTo create a validated array of numbers which allows array length to be from 0 to 10 and requires all array elements to be unique, you declare:\n\n```ts\nimport { VArray } from 'validated-types';\n\nfunction useArray(array: VArray\u003c'0,10,unique', number\u003e) {\n  // use array here\n  console.log(array.value);\n}\n\nconst array = VArray.tryCreate\u003c'0,10,unique', number\u003e('0,10,unique', [1, 2, 3]);\nconst maybeArray = VArray\u003c'0,10,unique', number\u003e.create('0,10,unique', [1, 2, 2]);\n\nuseArray(array); // prints to console: [1, 2, 3]\nuseArray(maybeArray ?? VArray.tryCreate('0,10,unique', [3, 4, 5])); // prints to console: [3, 4, 5]\n```\n\nYou can also create an array of validated objects, for example below example validates an array of from 1 to max 10 unique email addresses:\n```ts\nimport { VArray } from 'validated-types';\n\ntype EmailAddresses =  VArray\u003c'1,10,unique', VString\u003c'email'\u003e\u003e;\nconst emailAddressesVSpec: VSpecOf\u003cEmailAddresses\u003e = '1,10,unique';\n\nfunction sendEmails(emailAddresses: EmailAddresses, subject: string, body: string) {\n  console.log(emailAddresses.forEach(emailAddress=\u003e emailAddress.value));\n}\n\nconst emailAddress = VString.tryCreate\u003c'email'\u003e('email', 'test@example.com');\nconst emailAddress2 = VString.tryCreate\u003c'email'\u003e('email', 'test2@example.com');\nconst emailAddresses: EmailAddresses = VArray.tryCreate(emailAddressesVSpec, [emailAddress, emailAddress2]);\n\nsendEmails(emailAddresses, 'subj', 'body'); // prints to console: 'test@example.com' and 'test2@example.com'\n```\n\n### \u003ca name=\"validated-objects\"\u003e\u003c/a\u003e Validate Objects\n\nYou can also create validated objects, for example:\n\n```ts\ntype Person = {\n  firstName: VString\u003c1, 64\u003e;\n  lastName: VString\u003c1, 64\u003e;\n  nickNames: VString\u003c1, 64\u003e[];\n  email: VString\u003c'email'\u003e;\n  password: VString\u003c'8,1024,strongPassword'\u003e;\n  age: VInt\u003c0, 255\u003e;\n  interestPercent: VFloat\u003c'0,100'\u003e;\n};\n```\n\nYou can assign type aliases to your validated types:\n\ntypes.ts\n\n```ts\nimport { VFloat, VInt, VString } from 'validated-types';\n\nexport type Name = VString\u003c1, 64\u003e;\nexport type Email = VString\u003c'email'\u003e;\nexport type Password = VString\u003c'8,1024,strongPassword'\u003e;\nexport type Age = VInt\u003c0, 255\u003e;\nexport type Percent = VFloat\u003c'0,100'\u003e;\n```\n\nperson.ts\n\n```ts\nimport { Age, Email, Name, Password, Percent } from 'types';\n\ntype Person = {\n  firstName: Name;\n  lastName: Name;\n  nickNames: Name[];\n  email: Email;\n  password: Password;\n  age: Age;\n  interestPercent: Percent;\n};\n```\n\n### \u003ca name=\"create-semantic-types\"\u003e\u003c/a\u003e Create Semantic Types/Variables\n\nSemantic types and variables let you differentiate between variables of same type. The differentiation is done\nby assigning a semantic name to the type. Following example declares two semantic variables of `boolean` type with two different semantic names.\n\n```ts\nimport { SemType } from 'validated-types';\n\ntype IsRecursiveCall = SemType\u003cboolean, 'isRecursiveCall'\u003e\ntype IsInternalCall = SemType\u003cboolean, 'isInternalCall'\u003e;\n\nfunction myFunc(isRecursiveCall: IsRecursiveCall, isInternalCall: IsInternalCall) {\n  console.log(isRecursiveCall.value);\n  console.log(isInternalCall.value);\n}\n\nconst isRecursiveCall = false;\nconst isInternalCall = true;\n\n// Only this will succeed\nmyFunc(new SemType({ isRecursiveCall }), new SemType({ isInternalCall }));\n\n// These will fail during compilation\nmyFunc(new SemType({ isInternalCall }), new SemType({ isRecursiveCall }));\nmyFunc(true, true);\nmyFunc(new SemType('isSomethingElse', true), new SemType('isInternalCall', true));\nmyFunc(new SemType('isRecursiveCall', false), new SemType('isSomethingElse', true));\nmyFunc(new SemType('isSomethingElse', true), new SemType('isSomethingElse', true));\n```\n\nYou can use a validated type as a semantic type also. In the below example, a function takes two semantic parameters\nwhich both have the same base type `VString\u003c'1,64'\u003e`.\n\n```ts\nimport { SemType, VString } from 'validated-types';\n\ntype Name = VString\u003c'1,64'\u003e;\nconst nameVSpec: VSpecOf\u003cName\u003e = '1,64';\ntype FirstName = SemType\u003cName, 'firstName'\u003e;\ntype LastName = SemType\u003cName, 'lastName'\u003e;\n\nfunction myFunc(firstName: FirstName, lastName: LastName) {\n  console.log(firstName.value);\n  console.log(lastName.value);\n}\n\nconst firstName: Name = VString.tryCreate(nameVSpec, 'John');\nconst lastName: Name = VString.tryCreate(nameVSpec, 'Doe');\n\ntype Name2 = VString\u003c'1,65'\u003e\nconst name2VSpec: VSpecOf\u003cName\u003e = '1,64';\nconst firstName2: Name2 = VString.tryCreate(name2VSpec, 'John');\nconst lastName2: Name2 = VString.tryCreate(name2VSpec, 'Doe');\n\n// Only this will succeed where 'firstName' and 'lastName' are given in correct order with correct validated type\nmyFunc(new SemType({ firstName }), new SemType({ lastName }));\n\n// These below will fail during compilation\nmyFunc(new SemType({ lastName }), new SemType({ firstName }));\nmyFunc(new SemType({ firstName: firstName2 }), new SemType({ lastName: lastName2 }));\nmyFunc('John', 'Doe');\nmyFunc(firstName, lastName);\n```\n\n### \u003ca name=\"create-semantic-validated-integers\"\u003e\u003c/a\u003e Create Semantic Validated Integers\n\n```ts\nimport { SemVInt } from 'validated-types';\n\ntype HttpPort = SemVInt\u003c'httpPort', '1,65535'\u003e;\nconst httpPort: HttpPort = SemVInt.tryCreate('httpPort', '1,65535', 8080);\nconsole.log(httpPort.value); // Prints 8080\n```\n\n### \u003ca name=\"create-semantic-validated-floats\"\u003e\u003c/a\u003e Create Semantic Validated Floats\n\n```ts\nimport { SemVFloat } from 'validated-types';\n\ntype LoanInterest = SemVFloat\u003c'loanInterest', '0,100'\u003e;\nconst loanInterest: LoanInterest = SemVFloat.tryCreate('loanInterest', '0,100', 4.99);\nconsole.log(loanInterest.value); // Prints 4.99\n```\n\n### \u003ca name=\"create-semantic-validated-strings\"\u003e\u003c/a\u003e Create Semantic Validated Strings\n\n```ts\nimport { SemVString } from 'validated-types';\n\ntype LoginUrl = SemVString\u003c'loginUrl', '1,8192,url'\u003e;\nconst loginUrl: LoginUrl = SemVString.tryCreate('loginUrl', '1,8192,url', \"https://server.com\");\nconsole.log(loginUrl.value) // Prints https://server.com\n```\n\n## \u003ca name=\"api-documentation\"\u003e\u003c/a\u003e API documentation\n\n### \u003ca name=\"vfloat\"\u003e\u003c/a\u003e VFloat\n\n```ts\nclass VFloat\u003cVSpec extends string\u003e {\n  static registerCustomValidator(validatorName: string, validateFunc: (value: number) =\u003e boolean): void;\n  \n  static createOrThrow(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | never;\n  static tryCreate(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | never;\n  static create(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | null;\n  static createOrError(\n    validationSpec: string,\n    value: number,\n    varName?: string\n  ): [VFloat\u003cVSpec\u003e, null] | [null, Error];\n  get value(): number;\n}\n```\n\n#### \u003ca name=\"vfloat-registerCustomValidator\"\u003e\u003c/a\u003e VFloat.registerCustomValidator\n`static registerCustomValidator(validatorName: string, validateFunc: (value: number) =\u003e boolean): void`\n\nRegisters a custom validator with given name. `validateFunc` receives a float value as parameter and returns boolean based on success of validation.\nYou must register your custom validator before using it in any of the `create` functions.\n\n#### \u003ca name=\"vfloat-createOrThrow\"\u003e\u003c/a\u003eVFloat.createOrThrow and VFloat.tryCreate\n`static createOrThrow(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | never`\n`static tryCreate(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | never`\n\nCreates a new validated float value object or throws a `ValidationError` exception if supplied value is invalid.\n`validationSpec` is a string of following form `'[\u003cminValue\u003e],[\u003cmaxValue\u003e] | negative | positive | custom:\u003ccustom_validator_name\u003e'`.\n\n`validationSpec` examples:\n\n- '0,100'\n- '100,'\n- ',0'\n- 'positive'\n- 'negative'\n- 'custom:isUsShoeSize'\n\nIf `minValue` is missing, `Number.MIN_VALUE` is used.\nIf `maxValue` is missing, `Number.MAX_VALUE` is used.\nIf `varName` is supplied, it is mentioned in possible `ValidationError` thrown.\n\n#### \u003ca name=\"vfloat-create\"\u003e\u003c/a\u003eVFloat.create\n`static create(validationSpec: string, value: number, varName?: string): VFloat\u003cVSpec\u003e | null`\n\nSame as `VFloat.createOrThrow`, but instead of throwing on validation failure, it returns `null`.\nThis method is useful if you don't care about the error message, but just want to know if validation succeeded or not.\n\n#### \u003ca name=\"vfloat-createOrError\"\u003e\u003c/a\u003eVFloat.createOrError\n`static createOrError(validationSpec: string, value: number, varName?: string): [VFloat\u003cVSpec\u003e, null] | [null, Error]`\n\nSame as `VFloat.createOrThrow`, but instead of throwing on validation failure, it returns a tuple `[VFloat, null]` on success and tuple `[null, Error]` on validation failure.\nThis method is useful for Go language style of programming where you get a 2-tuple return value where the last element in tuple contains the possible error.\nFor example:\n\n```ts\nconst [float, err] = VFloat.createOrError\u003c'1,10'\u003e('1,10', 1);\nif (err) {\n  // handle error here\n}\n```\n\n#### \u003ca name=\"vfloat-value\"\u003e\u003c/a\u003evalue\n`get value(): number`\n\nReturns the valid float value.\n\n### \u003ca name=\"vint\"\u003e\u003c/a\u003e VInt\n\n```ts\nclass VInt\u003cVSpec extends string\u003e {\n  static registerCustomValidator(validatorName: string, validateFunc: (value: number) =\u003e boolean): void;\n  static createOrThrow(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | never;\n  static tryCreate(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | never;\n  static create(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | null;\n  static createOrError(\n    validationSpec: string,\n    value: number,\n    varName?: string\n  ): [VInt\u003cVSpec\u003e, null] | [null, Error];\n  get value(): number;\n}\n```\n\n#### \u003ca name=\"vint-registerCustomValidator\"\u003e\u003c/a\u003eVInt.registerCustomValidator\n`static registerCustomValidator(validatorName: string, validateFunc: (value: number) =\u003e boolean): void`\n\nRegisters a custom validator with given name. `validateFunc` receives a number as parameter and returns boolean based on success of validation.\nYou must register your custom validator before using it in any of the `create` functions.\n\n#### \u003ca name=\"vint-createOrThrow\"\u003e\u003c/a\u003eVInt.createOrThrow and VInt.tryCreate\n`static createOrThrow(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | never`\n`static tryCreate(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | never`\n\nCreates a new validated integer value object or throws a `ValidationError` exception if supplied value is invalid.\n`validationSpec` is a string of following form `'[\u003cminValue\u003e],[\u003cmaxValue\u003e][,\u003cdivisibleByValue\u003e] | negative | positive | custom:\u003ccustom_validator_name\u003e'`.\n\n`validationSpec` examples:\n\n- '0,100'\n- '0,100,2'\n- '100,'\n- ',0'\n- ','\n- 'positive'\n- 'negative'\n- 'custom:isEven'\n\nIf `minValue` is missing, `Number.MIN_SAFE_INTEGER` is used.\nIf `maxValue` is missing, `Number.MAX_SAFE_INTEGER` is used.\nIf `varName` is supplied, it is mentioned in possible `ValidationError` thrown.\n\n#### \u003ca name=\"vint-create\"\u003e\u003c/a\u003eVInt.create\n`static create(validationSpec: string, value: number, varName?: string): VInt\u003cVSpec\u003e | null`\n\nSame as `VInt.createOrThrow`, but instead of throwing on validation failure, it returns `null`.\nThis method is useful if you don't care about the error message, but just want to know if validation succeeded or not.\n\n#### \u003ca name=\"vint-createOrError\"\u003e\u003c/a\u003e VInt.createOrError\n`static createOrError(validationSpec: string, value: number, varName?: string): [VInt\u003cVSpec\u003e, null] | [null, Error]`\n\nSame as `VInt.createOrThrow`, but instead of throwing on validation failure, it returns a tuple `[VInt, null]` on success and tuple `[null, Error]` on validation failure\nThis method is useful for Go language style of programming where you get a 2-tuple return value where the last element in tuple contains the possible error.\nFor example:\n\n```ts\nconst [int, err] = VInt.createOrError\u003c'1,10'\u003e('1,10', 1);\nif (err) {\n  // handle error here\n}\n```\n\n#### \u003ca name=\"vint-value\"\u003e\u003c/a\u003e value\n`get value(): number`\n\nReturns the valid integer value.\n\n### \u003ca name=\"vstring\"\u003e\u003c/a\u003e VString\n\n```ts\nclass VString\u003cVSpec extends string\u003e {\n  static registerCustomValidator(validatorName: string | string[], validateFunc: (value: string) =\u003e boolean): void;\n  static createOrThrow(validationSpec: string | string[], value: number, varName?: string): VString\u003cVSpec\u003e | never;\n  static tryCreate(validationSpec: string | string[], value: number, varName?: string): VString\u003cVSpec\u003e | never;\n  static create(validationSpec: string | string[], value: number, varName?: string): VString\u003cVSpec\u003e | null;\n  static createOrError(\n    validationSpec: string,\n    value: number,\n    varName?: string\n  ): [VString\u003cVSpec\u003e, null] | [null, Error];\n  get value(): string;\n}\n```\n\n#### \u003ca name=\"vstring-registerCustomValidator\"\u003e\u003c/a\u003eVString.registerCustomValidator\n`static registerCustomValidator(validatorName: string, validateFunc: (value: string) =\u003e boolean): void`\n\nRegisters a custom validator with given name. `validateFunc` receives a string value as parameter and returns boolean based on success of validation.\nYou must register your custom validator before using it in any of the `create` functions.\n\n#### \u003ca name=\"vstring-createOrThrow\"\u003e\u003c/a\u003eVString.createOrThrow and VString.tryCreate\n`static createOrThrow(validationSpec: string, value: number, varName?: string): VString\u003cVSpec\u003e | never`\n`static tryCreate(validationSpec: string, value: number, varName?: string): VString\u003cVSpec\u003e | never`\n\nCreates a new validated integer value object or throws a `ValidationError` exception if supplied value is invalid.\n`validationSpec` is a string of following form `'[\u003cminLength\u003e],\u003cmaxLength\u003e[,\u003cunknown_length_validator_name\u003e[,\u003cparameter\u003e]] | \u003cknown_length_validator_name\u003e | custom:\u003ccustom_validator_name\u003e'`.\n\nPossible value for `\u003cunknown_length_validator_name\u003e`:\n\n- alpha\n- alphanumeric\n- ascii\n- base32\n- base58\n- base64\n- dataUri\n- decimal\n- fqdn\n- md4\n- md5\n- sha1\n- sha256\n- sha384\n- sha512\n- crc32\n- crc32b\n- hex\n- ipv4Range\n- ipv6Range\n- json\n- lowercase\n- magnetUri\n- mongoId\n- numeric\n- octal\n- uppercase\n- strongPassword\n- url\n- includes (requires `parameter`)\n- match (requires a RegExp string `parameter`)\n- isOneOf (requires JSON string array `parameter`)\n- isNoneOf (requires JSON string array `parameter`)\n- startsWith (requires `parameter`)\n- endsWith (requires `parameter`)\n- numericRange (requires `parameter`in format: `\u003cminValue\u003e-\u003cmaxValue\u003e`, e.g. 1-65535)\n\nPossible values for `\u003cknown_length_validator_name\u003e`:\n\n- boolean\n- bic\n- btcAddress\n- creditCard\n- ean\n- email\n- ethereumAddress\n- hsl\n- hexColor\n- isin\n- iban\n- ipv4\n- ipv6\n- iso31661Alpha2\n- iso31661Alpha3\n- iso8601\n- isrc\n- issn\n- jwt\n- latLong\n- macAddress\n- mimeType\n- port\n- rgbColor\n- semVer\n- uuid\n- postalCode\n- creditCardExpiration\n- cvc\n- mobileNumber\n\nMore information about validators can be found in [validator.js documentation](https://github.com/validatorjs/validator.js/)\nIf you need a new built-in validator, please [open a new issue](https://github.com/pksilen/validated-types/issues) about that.\n\n`validationSpec` examples:\n\n- '0,100'\n- ',100'\n- '1,1024,url'\n- '1,1024,startsWith,https'\n- 'email'\n- 'custom:isSupplierName'\n\nIf `minLength` is missing, 0 is used.\nIf `varName` is supplied, it is mentioned in possible `ValidationError` thrown.\n\n#### \u003ca name=\"vstring-create\"\u003e\u003c/a\u003eVString.create\n`static create(validationSpec: string, value: number, varName?: string): VString\u003cVSpec\u003e | null`\n\nSame as `VString.createOrThrow`, but instead of throwing on validation failure, it returns `null`.\nThis method is useful if you don't care about the error message, but just want to know if validation succeeded or not.\n\n#### \u003ca name=\"vstring-createOrErrorr\"\u003e\u003c/a\u003eVString.createOrError\n`static createOrError(validationSpec: string, value: number, varName?: string): [VString\u003cVSpec\u003e, null] | [null, Error]`\n\nSame as `VString.createOrThrow`, but instead of throwing on validation failure, it returns a tuple `[VString, null]` on success and tuple `[null, Error]` on validation failure\nThis method is useful for Go language style of programming where you get a 2-tuple return value where the last element in tuple contains the possible error.\nFor example:\n\n```ts\nconst [str, err] = VString.createOrError\u003c'1,10'\u003e('1,10', 'abc');\nif (err) {\n  // handle error here\n}\n```\n\n#### \u003ca name=\"vstring-value\"\u003e\u003c/a\u003evalue\n`get value(): string`\n\nReturns the valid string value.\n\n### \u003ca name=\"varray\"\u003e\u003c/a\u003e VArray\n \n`T` is the type of elements in the array.\n\n```ts\nclass VArray\u003cVSpec extends string, T\u003e {\n  static registerCustomValidator(validatorName: string, validateFunc: (value: T[]) =\u003e boolean): void;\n  static createOrThrow(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | never;\n  static tryCreate(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | never;\n  static create(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | null;\n  static createOrError(\n    validationSpec: string,\n    value: T[],\n    varName?: string\n  ): [VArray\u003cVSpec, T\u003e, null] | [null, Error];\n  get value(): T[];\n}\n```\n\n#### \u003ca name=\"varray-registerCustomValidator\"\u003e\u003c/a\u003e VArray.registerCustomValidator\n`static registerCustomValidator(validatorName: string, validateFunc: (value: T[]) =\u003e boolean): void`\n\nRegisters a custom validator with given name. `validateFunc` receives an array as parameter and returns boolean based on success of validation.\nYou must register your custom validator before using it in any of the `create` functions.\n\n#### \u003ca name=\"varray-createOrThrow\"\u003e\u003c/a\u003eVArray.createOrThrow and VArray.tryCreate\n`static createOrThrow(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | never`\n`static tryCreate(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | never`\n\nCreates a new validated array value object or throws a `ValidationError` exception if supplied value is invalid.\n`validationSpec` is a string of following form `'[\u003cminLength\u003e],\u003cmaxLength\u003e[,unique] | custom:\u003ccustom_validator_name\u003e'`.\n\n`validationSpec` examples:\n\n- '0,100'\n- ',100'\n- '1,10,unique'\n- 'custom:includesSomething'\n\nIf `minLength` is missing, 0 is used.\nIf `varName` is supplied, it is mentioned in possible `ValidationError` thrown.\n\n#### \u003ca name=\"varray-create\"\u003e\u003c/a\u003eVArray.create\n`static create(validationSpec: string, value: T[], varName?: string): VArray\u003cVSpec, T\u003e | null`\n\nSame as `VFloat.createOrThrow`, but instead of throwing on validation failure, it returns `null`.\nThis method is useful if you don't care about the error message, but just want to know if validation succeeded or not.\n\n#### \u003ca name=\"varray-createOrError\"\u003e\u003c/a\u003eVArray.createOrError\n`static createOrError(validationSpec: string, value: T[], varName?: string): [VArray\u003cVSpec, T\u003e, null] | [null, Error]`\n\nSame as `VFloat.createOrThrow`, but instead of throwing on validation failure, it returns a tuple `[VArray, null]` on success and tuple `[null, Error]` on validation failure.\nThis method is useful for Go language style of programming where you get a 2-tuple return value where the last element in tuple contains the possible error.\nFor example:\n\n```ts\nconst [array, err] = VArray.createOrError\u003c'1,10', number\u003e('1,10', [1]);\nif (err) {\n  // handle error here\n}\n```\n\n#### \u003ca name=\"varray-value\"\u003e\u003c/a\u003evalue\n`get value(): T[]`\n\nReturns the validated array.\n\n### \u003ca name=\"SemType\"\u003e\u003c/a\u003e SemType\n`T` is the type of Semantic variable. \n\n```ts\ntype SemName\u003cN extends string\u003e = N extends `${infer Name}` ? `${Name}` : never;\n\nclass SemType\u003cT, N extends string\u003e {\n  constructor(semanticName: SemName\u003cN\u003e, value: T);\n  constructor(semType: { [K in SemName\u003cN\u003e]: T });\n  get value(): T;\n}\n```\n\n#### \u003ca name=\"SemType-constructor\"\u003e\u003c/a\u003e constructor\n`constructor(semanticName: SemName\u003cN\u003e, value: T)`  \n`constructor(semType: { [K in SemName\u003cN\u003e]: T });`\n\nCreates a new semantic variable with semantic name `semanticName` and with value of type `T`.\n\n#### \u003ca name=\"SemType-value\"\u003e\u003c/a\u003e value\n`get value(): T`\nGet the value of semantic variable.\n\n### \u003ca name=\"SemVInt\"\u003e\u003c/a\u003e SemVInt\n\n```ts\ntype SemName\u003cN extends string\u003e = N extends `${infer Name}` ? `${Name}` : never;\n\nclass SemVInt\u003cName extends string, VSpec extends string\u003e {\n  static tryCreate\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: IntValidationSpec\u003cVSpec\u003e,\n    value: number,\n    varName?: string\n  ): SemVInt\u003cName, VSpec\u003e | never; // Throws on error\n\n  static create\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: IntValidationSpec\u003cVSpec\u003e,\n    value: number\n  ): SemVInt\u003cName, VSpec\u003e | null; // Returns null on error\n\n  static createOrError\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: IntValidationSpec\u003cVSpec\u003e,\n    value: number,\n    varName?: string\n  ): [SemVInt\u003cName, VSpec\u003e, null] | [null, Error] // Returns a pair [null, Error] on error\n}\n```\n\n### \u003ca name=\"SemVFloat\"\u003e\u003c/a\u003e SemVFloat\n\n```ts\ntype SemName\u003cN extends string\u003e = N extends `${infer Name}` ? `${Name}` : never;\n\nclass SemVFloat\u003cName extends string, VSpec extends string\u003e {\n  static tryCreate\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: FloatValidationSpec\u003cVSpec\u003e,\n    value: number,\n    varName?: string\n  ): SemVFloat\u003cName, VSpec\u003e | never; // Throws on error\n\n  static create\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: FloatValidationSpec\u003cVSpec\u003e,\n    value: number\n  ): SemVFloat\u003cName, VSpec\u003e | null // Returns null on error\n\n  static createOrError\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: FloatValidationSpec\u003cVSpec\u003e,\n    value: number,\n    varName?: string\n  ): [SemVFloat\u003cName, VSpec\u003e, null] | [null, Error]; // Returns a pair [null, Error] on error\n}\n```\n\n### \u003ca name=\"SemVString\"\u003e\u003c/a\u003e SemVString\n\n```ts\ntype SemName\u003cN extends string\u003e = N extends `${infer Name}` ? `${Name}` : never;\n\nclass SemVString\u003cName extends string, VSpec extends string | string[]\u003e {\n  static tryCreate\u003cName extends string, VSpec extends string | string[]\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: string | string[],\n    value: string,\n    varName?: string\n  ): SemVString\u003cName, VSpec\u003e | never; // Throws on error\n\n  static create\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: string | string[],\n    value: string,\n    varName?: string\n  ): SemVString\u003cName, VSpec\u003e | null; // Returns null on error\n\n  static createOrError\u003cName extends string, VSpec extends string\u003e(\n    semanticName: SemName\u003cName\u003e,\n    validationSpec: string | string[],\n    value: string,\n    varName?: string\n  ): [SemVString\u003cName, VSpec\u003e, null] | [null, Error]; // Returns a pair [null, Error] on error\n}\n```\n### \u003ca name=\"vspecof\"\u003e\u003c/a\u003e VSpecOf\n\nExtracts the validation spec type of the validated type.\n\nFor example:\n\n```ts\ntype Month = VInt\u003c'1,12'\u003e;\nconst monthVSpec: VSpecOf\u003cMonth\u003e = '1,12';\nconst month: Month = VInt.tryCreate(monthVSpec, 1);\n\ntype Percent = VFloat\u003c'0,100'\u003e;\nconst percentVSpec: VSpecOf\u003cPercent\u003e = '0,100';\nconst percent: Percent = VFloat.tryCreate(percentVSpec, 25.0);\n\ntype Url = VString\u003c['1,1024,lowercase', 'url', 'startsWith,https', 'endsWith,.html']\u003e;\nconst urlVSpec: VSpecOf\u003cUrl\u003e = ['1,1024,lowercase', 'url', 'startsWith,https', 'endsWith,.html'];\nconst url: Url = VString.tryCreate(urlVSpec, 'https://server.domain.com:8080/index.html');\n```\n\n## \u003ca name=\"feedback\"\u003e\u003c/a\u003e Feedback\n\nIf you want to report a bug, please [create a new issue](https://github.com/pksilen/validated-types/issues) about that.\n\nIf you want to request a new feature, for example, a new type of validator for string, please [create a new issue](https://github.com/pksilen/validated-types/issues) about that.\n\n## \u003ca name=\"license\"\u003e\u003c/a\u003e License\n\n[MIT](https://github.com/pksilen/validated-types/blob/main/LICENSE)\n\n[license-badge]: https://img.shields.io/badge/license-MIT-green\n[license]: https://github.com/pksilen/validated-types/blob/master/LICENSE\n[version-badge]: https://img.shields.io/npm/v/validated-types.svg?style=flat-square\n[package]: https://www.npmjs.com/package/validated-types\n[downloads]: https://img.shields.io/npm/dm/validated-types\n[build]: https://img.shields.io/circleci/project/github/pksilen/validated-types/main.svg?style=flat-square\n[circleci]: https://circleci.com/gh/pksilen/validated-types/tree/main\n[coverage]: https://img.shields.io/codecov/c/github/pksilen/validated-types/main.svg?style=flat-square\n[codecov]: https://codecov.io/gh/pksilen/validated-types\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpksilen%2Fvalidated-types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpksilen%2Fvalidated-types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpksilen%2Fvalidated-types/lists"}