diff --git a/docs/api/array.md b/docs/api/array.md index 8a49575..bc85055 100644 --- a/docs/api/array.md +++ b/docs/api/array.md @@ -1,5 +1,478 @@ # Array +## Summary + +Our array schema is always dependent on others. Becasue, array is a collection of other data-types. You can find all available references here. + +## Table of Contents + +- [Schema & Validators](#schema-and-validators) +- [unique](#unique) +- [min](#min) +- [max](#max) +- [range](#range) +- [objects](#objects) +- [enums](#enums) +- [Conclusion](#conclusion) + +## Schema and Validators + +## `.unique()` + +This validator is used to check if the array has unique values or not. If the array has duplicate values, it will return an error. + +::: warning + +Please note that, this validator will only work for primitive values. If you have an array of objects, it will not work. + +::: + +::: code-group + +```TypeScript +// define a schema of an array of unique numbers +const listOfUniqueNumbers = a.array(a.number()).unique() + +// validate & parse the array +const resultOne = listOfUniqueNumbers.parse([1, 2, 3, 4, 5]) // valid +const resultTow = listOfUniqueNumbers.parse([1, 2, 3, 4, 5, 1]) // invalid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of unique numbers +const listOfUniqueNumbers = a.array(a.number()).unique() + +// validate & parse the array +const resultOne = listOfUniqueNumbers.parse([1, 2, 3, 4, 5]) // valid +const resultTow = listOfUniqueNumbers.parse([1, 2, 3, 4, 5, 1]) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + valid: true, + value: [1, 2, 3, 4, 5] +} + +{ + errors: [ + { + reason: "Array has duplicate values", + value: [1, 2, 3, 4, 5, 1] + } + ] +} +``` + +```bash [JavaScript] +{ + valid: true, + value: [1, 2, 3, 4, 5] +} + +{ + errors: [ + { + reason: "Array has duplicate values", + value: [1, 2, 3, 4, 5, 1] + } + ] +} +``` + +::: + +## `.min()` + +This validator is used to check if the array has a minimum number of elements or not. If the array has fewer elements than the specified number, it will return an error. + +::: code-group + +```TypeScript +// define a schema of an array of numbers with a minimum of 5 elements +const listOfNumbers = a.array(a.number()).min(5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3, 4, 5]) // invalid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5, 6]) // valid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of numbers with a minimum of 5 elements +const listOfNumbers = a.array(a.number()).min(5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3, 4, 5]) // invalid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5, 6]) // valid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + errors: [ + { + reason: "Array has fewer elements than the minimum", + value: [1, 2, 3, 4, 5] + } + ] +} + +{ + valid: true, + value: [1, 2, 3, 4, 5, 6] +} +``` + +```bash [JavaScript] +{ + errors: [ + { + reason: "Array has fewer elements than the minimum", + value: [1, 2, 3, 4, 5] + } + ] +} + +{ + valid: true, + value: [1, 2, 3, 4, 5, 6] +} +``` + +::: + +## `.max()` + +This validator is used to check if the array has a maximum number of elements or not. If the array has more elements than the specified number, it will return an error. + +::: code-group + +```TypeScript +// define a schema of an array of numbers with a maximum of 5 elements +const listOfNumbers = a.array(a.number()).max(5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3, 4, 5]) // valid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5, 6]) // invalid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of numbers with a maximum of 5 elements +const listOfNumbers = a.array(a.number()).max(5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3, 4, 5]) // valid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5, 6]) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + valid: true, + value: [1, 2, 3, 4, 5] +} + +{ + errors: [ + { + reason: "Array has more elements than the maximum", + value: [1, 2, 3, 4, 5, 6] + } + ] +} +``` + +```bash [JavaScript] +{ + valid: true, + value: [1, 2, 3, 4, 5] +} + +{ + errors: [ + { + reason: "Array has more elements than the maximum", + value: [1, 2, 3, 4, 5, 6] + } + ] +} +``` + +::: + +## `.range()` + +This validator is used to check if the array has a range of elements or not. If the array has fewer or more elements than the specified range, it will return an error. + +::: code-group + +```TypeScript +// define a schema of an array of numbers with a range of 3 to 5 elements +const listOfNumbers = a.array(a.number()).range(3, 5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3]) // invalid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5]) // valid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of numbers with a range of 3 to 5 elements +const listOfNumbers = a.array(a.number()).range(3, 5) + +// validate & parse the array +const resultOne = listOfNumbers.parse([1, 2, 3]) // invalid +const resultTow = listOfNumbers.parse([1, 2, 3, 4, 5]) // valid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + errors: [ + { + reason: "Array has fewer elements than the minimum", + value: [1, 2, 3] + } + ] +} + +{ + valid: true, + value: [1, 2, 3, 4, 5] +} +``` + +```bash [JavaScript] +{ + errors: [ + { + reason: "Array has fewer elements than the minimum", + value: [1, 2, 3] + } + ] +} + +{ + valid: true, + value: [1, 2, 3, 4, 5] +} +``` + +::: + +## Objects + +This validator is used to check if the array has a specific object schema or not. If the array has an object that does not match the specified schema, it will return an error. + +::: code-group + +```TypeScript +// define a schema of an array of objects +const listOfObjects = a.array(a.object({ + name: a.string(), + age: a.number() +})) + +// validate & parse the array +const resultOne = listOfObjects.parse([ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: 23 } +]) // valid + +const resultTow = listOfObjects.parse([ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: '23' } +]) // invalid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of objects +const listOfObjects = a.array(a.object({ + name: a.string(), + age: a.number() +})) + +// validate & parse the array +const resultOne = listOfObjects.parse([ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: 23 } +]) // valid + +const resultTow = listOfObjects.parse([ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: '23' } +]) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + valid: true, + value: [ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: 23 } + ] +} + +{ + errors: [ + { + reason: "Invalid value for age", + value: [ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: '23' } + ] + } + ] +} +``` + +```bash [JavaScript] +{ + valid: true, + value: [ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: 23 } + ] +} + +{ + errors: [ + { + reason: "Invalid value for age", + value: [ + { name: 'John Doe', age: 25 }, + { name: 'Jane Doe', age: '23' } + ] + } + ] +} +``` + +::: tip + +You can also use the `min`, `max`, `range` validators with this object based schema here. + +::: + +## Enums + +This validator is used to check if the array has specific values or not. If the array has a value that is not in the specified list, it will return an error. + ::: info -Work in progress ⏳ + +Enums are like experimental feature for now. It may not work as expected. We are working on it to make it better. If you find any issue, please raise an issue [here](https://github.com/mahabubx7/akar/issues). + +::: + +::: code-group + +```TypeScript +// define a schema of an array of numbers with specific values +const listOfStatus = a.array(a.enum(['active', 'inactive'] as const)) + +// validate & parse the array +const resultOne = listOfStatus.parse(['active', 'inactive']) // valid +const resultTow = listOfStatus.parse(['active', 'inactive', 'pending']) // invalid + +console.log(resultOne, resultTow) +``` + +```JavaScript +// define a schema of an array of numbers with specific values +const listOfStatus = a.array(a.enum(['active', 'inactive'])) + +// validate & parse the array +const resultOne = listOfStatus.parse(['active', 'inactive']) // valid +const resultTow = listOfStatus.parse(['active', 'inactive', 'pending']) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [TypeScript] +{ + valid: true, + value: ['active', 'inactive'] +} + +{ + errors: [ + { + reason: "Invalid value", + value: ['active', 'inactive', 'pending'] + } + ] +} +``` + +```bash [JavaScript] +{ + valid: true, + value: ['active', 'inactive'] +} + +{ + errors: [ + { + reason: "Invalid value", + value: ['active', 'inactive', 'pending'] + } + ] +} +``` + ::: + +## Conclusion + +This is the end of the array schema. You can raise an issue [here](https://github.com/mahabubx7/akar/issues) if you find any problem. We will try to fix it as soon as possible. diff --git a/docs/api/boolean.md b/docs/api/boolean.md index 2786843..4c93d2c 100644 --- a/docs/api/boolean.md +++ b/docs/api/boolean.md @@ -1,5 +1,101 @@ # Boolean -::: info -Work in progress ⏳ +## Summary + +Boolean is a data type that can have one of two values: `true` or `false`. + +## Table of Contents + +- [Schema & Validators](#schema--validators) +- [default](#default) +- [exact](#exact) +- [Conclusion](#conclusion) + +## Schema & Validators + +## default + +By default, the boolean schema does not have any validators. It only checks if the value is a boolean. + +::: code-group + +```typescript +// define a schema for boolean +const booleanSchema = a.boolean() + +// validate & parse the boolean +const resultOne = booleanSchema.parse(true) // valid +const resultTow = booleanSchema.parse(false) // valid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for boolean +const booleanSchema = a.boolean() + +// validate & parse the boolean +const resultOne = booleanSchema.parse(true) // valid +const resultTow = booleanSchema.parse(false) // valid + +console.log(resultOne, resultTow) +``` + ::: + +**Output** + +```bash +{ value: true } + +{ value: false } +``` + +## `.exact(value: boolean)` + +The `exact` validator checks if the value is exactly equal to the provided value. + +::: code-group + +```typescript +// define a schema for boolean with exact validator +const booleanSchema = a.boolean().exact(true) + +// validate & parse the boolean +const resultOne = booleanSchema.parse(true) // valid +const resultTow = booleanSchema.parse(false) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for boolean with exact validator +const booleanSchema = a.boolean().exact(true) + +// validate & parse the boolean +const resultOne = booleanSchema.parse(true) // valid +const resultTow = booleanSchema.parse(false) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: true } + +{ + errors: [ + { + reason: 'Value must be true', + value: false + } + ] +} +``` + +## Conclusion + +In this chapter, we learned about the Boolean data type. We learned that it can have one of two values: `true` or `false`. diff --git a/docs/api/enum.md b/docs/api/enum.md index ced2b34..288f572 100644 --- a/docs/api/enum.md +++ b/docs/api/enum.md @@ -1,5 +1,132 @@ # Enum -::: info -Work in progress ⏳ +## Summary + +Enum is a data type that can have one of a specific set of values. It is similar to a string, but it is limited to a specific set of values. + +## Table of Contents + +- [Schema & Validators](#schema--validators) +- [default](#default) +- [Conclusion](#conclusion) + +## Schema & Validators + +::: code-group + +```typescript +// define a schema for enum +const enumSchema = a.enum(["red", "green", "blue"] as const) + +// validate & parse the enum +const resultOne = enumSchema.parse("red") // valid +const resultTow = enumSchema.parse("green") // valid +const resultThree = enumSchema.parse("blue") // valid +/* + * You might not be able to insert any other value other than the specified values + * while writing the code in TypeScript, but you can insert any value in JavaScript. Specify the values as const to prevent this. + */ +// const resultFour = enumSchema.parse("yellow") // invalid + +console.log(resultOne, resultTow, resultThree) +``` + +```javascript +// define a schema for enum +const enumSchema = a.enum(["red", "green", "blue"]) + +// validate & parse the enum +const resultOne = enumSchema.parse("red") // valid +const resultTow = enumSchema.parse("green") // valid +const resultThree = enumSchema.parse("blue") // valid +/* + * You might not be able to insert any other value other than the specified values + * while writing the code in TypeScript, but you can insert any value in JavaScript. Specify the values as const to prevent this. + */ +const resultFour = enumSchema.parse("yellow") // invalid + +console.log(resultOne, resultTow, resultThree, resultFour) +``` + +::: + +**Output** + +::: code-group + +```bash [typescript] +{ value: 'red' } + +{ value: 'green' } + +{ value: 'blue' } +``` + +```bash [javascript] +{ value: 'red' } + +{ value: 'green' } + +{ value: 'blue' } + +{ + errors: [ + { + reason: 'Value must be one of the specified values', + value: 'yellow' + } + ] +} +``` + ::: + +## `.default(value: string)` + +The `default` validator sets the default value of the schema if the value is `undefined`. + +::: code-group + +```typescript +// define a schema for enum with default value +const enumSchema = a.enum(["red", "green", "blue"] as const).default("red") + +// validate & parse the enum +const resultOne = enumSchema.parse(undefined) // valid +const resultTow = enumSchema.parse("green") // valid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for enum with default value +const enumSchema = a.enum(["red", "green", "blue"]).default("red") + +// validate & parse the enum +const resultOne = enumSchema.parse(undefined) // valid +const resultTow = enumSchema.parse("green") // valid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +::: code-group + +```bash [typescript] +{ value: 'red' } + +{ value: 'green' } +``` + +```bash [javascript] +{ value: 'red' } + +{ value: 'green' } +``` + +## Conclusion + +This is the end of the Enum documentation. If you have any questions or suggestions, please feel free to open an issue on the [GitHub repository](https://github.com/mahabubx7/akar) diff --git a/docs/api/index.md b/docs/api/index.md index 27d6ff8..fdc016a 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -4,6 +4,11 @@ Welcome to the `Akar.js`. This page will walk you through the available API references of this library. -::: info -Work in progress ⏳ -::: +## Table of Contents + +- [Array](/api/array) +- [Object](/api/object) +- [String](/api/string) +- [Number](/api/number) +- [Enum](/api/enum) +- [Boolean](/api/boolean) diff --git a/docs/api/number.md b/docs/api/number.md index 0cf9515..385d89e 100644 --- a/docs/api/number.md +++ b/docs/api/number.md @@ -1,5 +1,753 @@ # Number -::: info -Work in progress ⏳ +## Summary + +You can define a schema of a number using the `a.number()` method. This method will return a schema object that can be used to validate and parse the number. + +## Table of Contents + +- [Schema & Validators](#schema-and-validators) +- [min](#min) +- [max](#max) +- [range](#range) +- [integer](#integer) +- [float](#float) +- [unsigned](#unsigned) +- [signed](#signed) +- [odd](#odd) +- [even](#even) +- [divisibleBy](#divisibleby) +- [port](#port) +- [binary](#binary) +- [octal](#octal) +- [hex](#hex) +- [prime](#prime) +- [perfect](#perfect) +- [Conclusion](#conclusion) + +## Schema and Validators + +## `.min(value: number)` + +This validator is used to check if the number is greater than or equal to the specified value. + +::: code-group + +```typescript +// define a schema of a number greater than or equal to 10 +const numberSchema = a.number().min(10) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a number greater than or equal to 10 +const numberSchema = a.number().min(10) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Minimum value is 10', + value: 5 + } + ] +} +``` + +## `.max(value: number)` + +This validator is used to check if the number is less than or equal to the specified value. + +::: code-group + +```typescript +// define a schema of a number less than or equal to 10 +const numberSchema = a.number().max(10) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(15) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a number less than or equal to 10 +const numberSchema = a.number().max(10) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(15) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Maximum value is 10', + value: 15 + } + ] +} +``` + +## `.range(min: number, max: number)` + +This validator is used to check if the number is within the specified range. + +::: code-group + +```typescript +// define a schema of a number within the range of 10 and 20 +const numberSchema = a.number().range(10, 20) + +// validate & parse the number +const resultOne = numberSchema.parse(15) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a number within the range of 10 and 20 +const numberSchema = a.number().range(10, 20) + +// validate & parse the number +const resultOne = numberSchema.parse(15) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 15 } + +{ + errors: [ + { + reason: 'Number is not within the range', + value: 5 + } + ] +} +``` + +## `.integer()` + +This validator is used to check if the number is an integer. + +::: code-group + +```typescript +// define a schema of an integer number +const numberSchema = a.number().integer() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(10.5) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an integer number +const numberSchema = a.number().integer() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(10.5) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Number is not an integer', + value: 10.5 + } + ] +} +``` + +## `.float()` + +This validator is used to check if the number is a float. + +::: code-group + +```typescript +// define a schema of a float number +const numberSchema = a.number().float() + +// validate & parse the number +const resultOne = numberSchema.parse(10.5) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a float number +const numberSchema = a.number().float() + +// validate & parse the number +const resultOne = numberSchema.parse(10.5) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10.5 } + +{ + errors: [ + { + reason: 'Number is not a float', + value: 10 + } + ] +} +``` + +## `.unsigned()` + +This validator is used to check if the number is an unsigned number. + +::: code-group + +```typescript +// define a schema of an unsigned number +const numberSchema = a.number().unsigned() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(-10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an unsigned number +const numberSchema = a.number().unsigned() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(-10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Number is not an unsigned number', + value: -10 + } + ] +} +``` + +## `.signed()` + +This validator is used to check if the number is a signed number. + +::: code-group + +```typescript +// define a schema of a signed number +const numberSchema = a.number().signed() + +// validate & parse the number +const resultOne = numberSchema.parse(-10) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a signed number +const numberSchema = a.number().signed() + +// validate & parse the number +const resultOne = numberSchema.parse(-10) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: -10 } + +{ + errors: [ + { + reason: 'Number is not a signed number', + value: 10 + } + ] +} +``` + +## `.odd()` + +This validator is used to check if the number is an odd number. + +::: code-group + +```typescript +// define a schema of an odd number +const numberSchema = a.number().odd() + +// validate & parse the number +const resultOne = numberSchema.parse(5) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an odd number +const numberSchema = a.number().odd() + +// validate & parse the number +const resultOne = numberSchema.parse(5) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 5 } + +{ + errors: [ + { + reason: 'Number is not an odd number', + value: 10 + } + ] +} +``` + +## `.even()` + +This validator is used to check if the number is an even number. + +::: code-group + +```typescript +// define a schema of an even number +const numberSchema = a.number().even() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an even number +const numberSchema = a.number().even() + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(5) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Number is not an even number', + value: 5 + } + ] +} +``` + +## `.divisibleBy(value: number)` + +This validator is used to check if the number is divisible by the specified value. + +::: code-group + +```typescript +// define a schema of a number divisible by 5 +const numberSchema = a.number().divisibleBy(5) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(7) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a number divisible by 5 +const numberSchema = a.number().divisibleBy(5) + +// validate & parse the number +const resultOne = numberSchema.parse(10) // valid +const resultTow = numberSchema.parse(7) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 10 } + +{ + errors: [ + { + reason: 'Number is not divisible by 5', + value: 7 + } + ] +} +``` + +## `.port()` + +This validator is used to check if the number is a valid port number. + +::: code-group + +```typescript +// define a schema of a valid port number +const numberSchema = a.number().port() + +// validate & parse the number +const resultOne = numberSchema.parse(80) // valid +const resultTow = numberSchema.parse(65536) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a valid port number + +const numberSchema = a.number().port() + +// validate & parse the number +const resultOne = numberSchema.parse(80) // valid +const resultTow = numberSchema.parse(65536) // invalid + +console.log(resultOne, resultTow) +``` + ::: + +**Output** + +```bash +{ value: 80 } + +{ + errors: [ + { + reason: 'Number is not a valid port number', + value: 65536 + } + ] +} +``` + +## `.binary()` + +This validator is used to check if the number is a valid binary number. + +::: code-group + +```typescript +// define a schema of a valid binary number +const numberSchema = a.number().binary() + +// validate & parse the number +const resultOne = numberSchema.parse(1010) // valid +const resultTow = numberSchema.parse(1234) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a valid binary number +const numberSchema = a.number().binary() + +// validate & parse the number +const resultOne = numberSchema.parse(1010) // valid +const resultTow = numberSchema.parse(1234) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 1010 } + +{ + errors: [ + { + reason: 'Number is not a valid binary number', + value: 1234 + } + ] +} +``` + +## `.octal()` + +This validator is used to check if the number is a valid octal number. + +::: code-group + +```typescript +// define a schema of a valid octal number +const numberSchema = a.number().octal() + +// validate & parse the number +const resultOne = numberSchema.parse(1234) // valid +const resultTow = numberSchema.parse(1010) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a valid octal number +const numberSchema = a.number().octal() + +// validate & parse the number +const resultOne = numberSchema.parse(1234) // valid +const resultTow = numberSchema.parse(1010) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 1234 } + +{ + errors: [ + { + reason: 'Number is not a valid octal number', + value: 1010 + } + ] +} +``` + +## `.hex()` + +This validator is used to check if the number is a valid hexadecimal number. + +::: code-group + +```typescript +// define a schema of a valid hexadecimal number +const numberSchema = a.number().hex() + +// validate & parse the number +const resultOne = numberSchema.parse(0x1234) // valid +const resultTow = numberSchema.parse(0x123g) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a valid hexadecimal number +const numberSchema = a.number().hex() + +// validate & parse the number +const resultOne = numberSchema.parse(0x1234) // valid +const resultTow = numberSchema.parse(0x123g) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 4660 } + +{ + errors: [ + { + reason: 'Number is not a valid hexadecimal number', + value: 0x123g + } + ] +} +``` + +## `.prime()` + +This validator is used to check if the number is a prime number. + +::: code-group + +```typescript +// define a schema of a prime number +const numberSchema = a.number().prime() + +// validate & parse the number +const resultOne = numberSchema.parse(7) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a prime number +const numberSchema = a.number().prime() + +// validate & parse the number +const resultOne = numberSchema.parse(7) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 7 } + +{ + errors: [ + { + reason: 'Number is not a prime number', + value: 10 + } + ] +} +``` + +## `.perfect()` + +This validator is used to check if the number is a perfect number. + +::: code-group + +```typescript +// define a schema of a perfect number +const numberSchema = a.number().perfect() + +// validate & parse the number +const resultOne = numberSchema.parse(28) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of a perfect number +const numberSchema = a.number().perfect() + +// validate & parse the number +const resultOne = numberSchema.parse(28) // valid +const resultTow = numberSchema.parse(10) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 28 } + +{ + errors: [ + { + reason: 'Number is not a perfect number', + value: 10 + } + ] +} +``` + +## Conclusion + +In this guide, you learned how to define a schema of a number using the `a.number()` method. You also learned about various validators that can be used to validate and parse the number. You can now use this knowledge to define a schema of a number in your application. diff --git a/docs/api/object.md b/docs/api/object.md index 34db6da..f62bd3e 100644 --- a/docs/api/object.md +++ b/docs/api/object.md @@ -1,5 +1,312 @@ # Object -::: info -Work in progress ⏳ +The `Object` class is the base class for all objects in the game. It provides a number of methods and properties that are common to all objects. + +## Table of Contents + +- [Schema & Validators](#schema-and-validators) +- [EqualTo](#equal-to) +- [DeepEqualTo](#not-equal-to) +- [ShallowEqualTo](#shallow-equal-to) +- [JsonObject](#json-object) +- [Conclusion](#conclusion) + +## Schema and Validators + +## `.equalTo()` + +This validator is used to check if the object is equal to the provided object or not. If the object is not equal to the provided object, it will return an error. + +::: code-group + +```typescript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .equalTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .equalTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ name: 'John Doe', age: 30 } + +{ + errors: [ + { + reason: 'Object must be equal to "{ name: \'John Doe\', age: 30 }"', + value: { name: 'Jane Doe', age: 25 } + } + ] +} +``` + +## `.deepEqualTo()` + +This validator is used to check if the object is deeply equal to the provided object or not. If the object is not deeply equal to the provided object, it will return an error. + +::: code-group + +```typescript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .deepEqualTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .deepEqualTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ name: 'John Doe', age: 30 } + +{ + errors: [ + { + reason: 'Object must be deeply equal!', + value: { name: 'Jane Doe', age: 25 } + } + ] +} +``` + +## `.shallowEqualTo()` + +This validator is used to check if the object is shallowly equal to the provided object or not. If the object is not shallowly equal to the provided object, it will return an error. + +::: code-group + +```typescript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .shallowEqualTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .shallowEqualTo({ + name: "John Doe", + age: 30 + }) + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: 25 +}) // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ name: 'John Doe', age: 30 } + + { + errors: [ + { + reason: 'Object must be shallowly equal!', + value: { name: 'Jane Doe', age: 25 } + } + ] + } +``` + +## `.jsonObject()` + +This validator is used to check if the object is a valid JSON object or not. If the object is not a valid JSON object, it will return an error. + +::: code-group + +```typescript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .jsonObject() + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: "25" +}) // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema of an object +const user = a + .object({ + name: a.string(), + age: a.number() + }) + .jsonObject() + +// validate & parse the object +const resultOne = user.parse({ + name: "John Doe", + age: 30 +}) // valid + +const resultTow = user.parse({ + name: "Jane Doe", + age: "25" +}) // invalid + +console.log(resultOne, resultTow) +``` + ::: + +**Output** + +```bash +{ name: 'John Doe', age: 30 } + +{ + errors: [ + { + reason: 'Object must be a valid JSON object!', + value: { name: 'Jane Doe', age: '25' } + } + ] +} +``` + +## Conclusion + +We covered the latest `Object` schema references so far. We will keep updating the documentation as we add more features to the `Object` schema. If you have any questions or suggestions, please feel free to raise an issue. diff --git a/docs/api/string.md b/docs/api/string.md index dc0be9b..7b17cd6 100644 --- a/docs/api/string.md +++ b/docs/api/string.md @@ -1,5 +1,2694 @@ # String -::: info -Work in progress ⏳ +## Summary + +String is a data type that represents a sequence of characters. It is used to store text data. We already covered a lot of things about the string data type in the schema. In this documentation, we will discuss the avaliable string schema and its validators. + +## Table of Contents + +- [Schema & Validators](#schema--validators) +- [Min](#min) +- [Max](#max) +- [Pattern](#pattern) +- [URL](#url) +- [Aplha](#alpha) +- [AlphaNumeric](#alphanumeric) +- [Numeric](#numeric) +- [Lowercase](#lowercase) +- [Uppercase](#uppercase) +- [Phone](#phone) +- [Hex](#hex) +- [Base64](#base64) +- [Base64URL](#base64url) +- [IP](#ip) +- [IPv4](#ipv4) +- [IPv6](#ipv6) +- [UUID](#uuid) +- [MongoID](#mongoid) +- [GUID](#guid) +- [CUID](#cuid) +- [JSON](#json) +- [Mac](#mac) +- [CreditCard](#creditcard) +- [Country](#country) +- [PostalCode](#postalcode) +- [Passport](#passport) +- [Password](#password) +- [Currency](#currency) +- [Data URI](#data-uri) +- [MIME Type](#mime-type) +- [LatLong](#latlong) +- [SemVer](#semver) +- [MD5](#md5) +- [SHA1](#sha1) +- [SHA256](#sha256) +- [SHA512](#sha512) +- [JWT](#jwt) +- [IBAN](#iban) +- [BIC](#bic) +- [ISIN](#isin) +- [Hex Color](#hex-color) +- [RGB Color](#rgb-color) +- [HSL Color](#hsl-color) +- [Locale](#locale) +- [OTP](#otp) +- [Slug](#slug) +- [FQDN](#fqdn) +- [Variable Name](#variable-name) +- [Date](#date) +- [Time](#time) +- [Hour](#hour) +- [Minute Or Seconds](#minute-or-seconds) +- [Timezone](#timezone) +- [Weekday](#weekday) +- [Base](#base) +- [Contains](#contains) +- [MagnetURI](#magneturi) +- [Conclusion](#conclusion) + +## Schema & Validators + +## `min()` + +The `min` validator checks if the string length is greater than or equal to the provided value. + +::: code-group + +```typescript +// define a schema for string with min validator +const stringSchema = a.string().min(5) + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("hi") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with min validator +const stringSchema = a.string().min(5) + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("hi") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hello' } + +{ + errors: [ + { + reason: 'Minimum length is 5', + value: 'hi', + } + ] +} +``` + +## `max()` + +The `max` validator checks if the string length is less than or equal to the provided value. + +::: code-group + +```typescript +// define a schema for string with max validator +const stringSchema = a.string().max(5) + +// validate & parse the string +const resultOne = stringSchema.parse("hi") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with max validator +const stringSchema = a.string().max(5) + +// validate & parse the string +const resultOne = stringSchema.parse("hi") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hi' } + +{ + errors: [ + { + reason: 'Maximum length is 5', + value: 'hello', + } + ] +} +``` + +## `pattern()` + +The `pattern` validator checks if the string matches the provided regular expression pattern. + +::: code-group + +```typescript +// define a schema for string with pattern validator +const stringSchema = a.string().pattern(/^[a-z]+$/) + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("0123") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with pattern validator +const stringSchema = a.string().pattern(/^[a-z]+$/) + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("0123") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hello' } + +{ + errors: [ + { + reason: 'Pattern does not match', + value: '0123', + } + ] +} +``` + +## `url()` + +The `url` validator checks if the string is a valid URL. + +::: code-group + +```typescript +// define a schema for string with url validator +const stringSchema = a.string().url() + +// validate & parse the string +const resultOne = stringSchema.parse("https://example.com") // valid +const resultTow = stringSchema.parse("http-example-com") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with url validator +const stringSchema = a.string().url() + +// validate & parse the string +const resultOne = stringSchema.parse("https://example.com") // valid +const resultTow = stringSchema.parse("http-example-com") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'https://example.com' } + +{ + errors: [ + { + reason: 'Value must be a valid URL', + value: 'http-example-com', + } + ] +} +``` + +## `alpha()` + +The `alpha` validator checks if the string contains only alphabetic characters. + +::: code-group + +```typescript +// define a schema for string with alpha validator +const stringSchema = a.string().alpha() + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("hello123") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with alpha validator +const stringSchema = a.string().alpha() + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("hello123") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hello' } + +{ + errors: [ + { + reason: 'Value must contain only alphabetic characters', + value: 'hello123', + } + ] +} +``` + +## `alphanumeric()` + +The `alphanumeric` validator checks if the string contains only alphabetic and numeric characters. + +::: code-group + +```typescript +// define a schema for string with alphanumeric validator +const stringSchema = a.string().alphanumeric() + +// validate & parse the string +const resultOne = stringSchema.parse("hello123") // valid +const resultTow = stringSchema.parse("hello@123") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with alphanumeric validator +const stringSchema = a.string().alphanumeric() + +// validate & parse the string +const resultOne = stringSchema.parse("hello123") // valid +const resultTow = stringSchema.parse("hello@123") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hello123' } + +{ + errors: [ + { + reason: 'Value must contain only alphanumeric characters', + value: 'hello@123', + } + ] +} +``` + +## `numeric()` + +The `numeric` validator checks if the string contains only numeric characters. + +::: code-group + +```typescript +// define a schema for string with numeric validator +const stringSchema = a.string().numeric() + +// validate & parse the string +const resultOne = stringSchema.parse("123") // valid +const resultTow = stringSchema.parse("123abc") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with numeric validator +const stringSchema = a.string().numeric() + +// validate & parse the string +const resultOne = stringSchema.parse("123") // valid +const resultTow = stringSchema.parse("123abc") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: '123' } + +{ + errors: [ + { + reason: 'Value must contain only numeric characters', + value: '123abc', + } + ] +} +``` + +## `lowercase()` + +The `lowercase` validator checks if the string contains only lowercase characters. + +::: code-group + +```typescript +// define a schema for string with lowercase validator +const stringSchema = a.string().lowercase() + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("Hello") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with lowercase validator +const stringSchema = a.string().lowercase() + +// validate & parse the string +const resultOne = stringSchema.parse("hello") // valid +const resultTow = stringSchema.parse("Hello") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'hello' } + +{ + errors: [ + { + reason: 'Value must contain only lowercase characters', + value: 'Hello', + } + ] +} +``` + +## `uppercase()` + +The `uppercase` validator checks if the string contains only uppercase characters. + +::: code-group + +```typescript +// define a schema for string with uppercase validator +const stringSchema = a.string().uppercase() + +// validate & parse the string +const resultOne = stringSchema.parse("HELLO") // valid +const resultTow = stringSchema.parse("Hello") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with uppercase validator +const stringSchema = a.string().uppercase() + +// validate & parse the string +const resultOne = stringSchema.parse("HELLO") // valid +const resultTow = stringSchema.parse("Hello") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'HELLO' } + +{ + errors: [ + { + reason: 'Value must contain only uppercase characters', + value: 'Hello', + } + ] +} +``` + +## `phone()` + +The `phone` validator checks if the string is a valid phone number. + +::: code-group + +```typescript +// define a schema for string with phone validator +const stringSchema = a.string().phone() + +// validate & parse the string +const resultOne = stringSchema.parse("+8801234567890") // valid +const resultTow = stringSchema.parse("1234567890") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with phone validator +const stringSchema = a.string().phone() + +// validate & parse the string +const resultOne = stringSchema.parse("+8801234567890") // valid +const resultTow = stringSchema.parse("1234567890") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: '+8801234567890' } + +{ + errors: [ + { + reason: 'Value must be a valid phone number', + value: '1234567890', + } + ] +} +``` + +## `hex()` + +The `hex` validator checks if the string is a valid hexadecimal number. + +::: code-group + +```typescript +// define a schema for string with hex validator +const stringSchema = a.string().hex() + +// validate & parse the string +const resultOne = stringSchema.parse("0x1234567890abcdef") // valid +const resultTow = stringSchema.parse("1234567890abcdef") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with hex validator +const stringSchema = a.string().hex() + +// validate & parse the string +const resultOne = stringSchema.parse("0x1234567890abcdef") // valid +const resultTow = stringSchema.parse("1234567890abcdef") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: '0x1234567890abcdef' } + +{ + errors: [ + { + reason: 'Value must be a valid hexadecimal number', + value: '1234567890abcdef', + } + ] +} +``` + +## `base64()` + +The `base64` validator checks if the string is a valid base64 encoded string. + +::: code-group + +```typescript +// define a schema for string with base64 validator +const stringSchema = a.string().base64() + +// validate & parse the string +const resultOne = stringSchema.parse("aGVsbG8gd29ybGQ=") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with base64 validator +const stringSchema = a.string().base64() + +// validate & parse the string +const resultOne = stringSchema.parse("aGVsbG8gd29ybGQ=") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'aGVsbG8gd29ybGQ=' } + +{ + errors: [ + { + reason: 'Value must be a valid base64 encoded string', + value: 'hello', + } + ] +} +``` + +## `base64url()` + +The `base64url` validator checks if the string is a valid base64url encoded string. + +::: code-group + +```typescript +// define a schema for string with base64url validator +const stringSchema = a.string().base64url() + +// validate & parse the string +const resultOne = stringSchema.parse("aGVsbG8gd29ybGQ") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with base64url validator +const stringSchema = a.string().base64url() + +// validate & parse the string +const resultOne = stringSchema.parse("aGVsbG8gd29ybGQ") // valid +const resultTow = stringSchema.parse("hello") // invalid + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: 'aGVsbG8gd29ybGQ' } + +{ + errors: [ + { + reason: 'Value must be a valid base64url encoded string', + value: 'hello', + } + ] +} +``` + +## `ip()` + +The `ip` validator checks if the string is a valid IP address. + +::: code-group + +```typescript +// define a schema for string with ip validator +const stringSchema = a.string().ip() + +// validate & parse the string +const resultOne = stringSchema.parse("103.101.102.104") +const resultTow = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") +const resultThree = stringSchema.parse("example.com") + +console.log(resultOne, resultTow, resultThree) +``` + +```javascript +// define a schema for string with ip validator +const stringSchema = a.string().ip() + +// validate & parse the string +const resultOne = stringSchema.parse("103.101.102.104") +const resultTow = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") +const resultThree = stringSchema.parse("example.com") + +console.log(resultOne, resultTow, resultThree) +``` + +::: + +**Output** + +```bash + { value: '103.101.102.104' } + + { value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334' } + + { + errors: [ + { + reason: 'Value must be a valid IP address', + value: 'example.com', + } + ] + } +``` + +## `ipv4()` + +The `ipv4` validator checks if the string is a valid IPv4 address. + +::: code-group + +```typescript +// define a schema for string with ipv4 validator +const stringSchema = a.string().ipv4() + +// validate & parse the string +const resultOne = stringSchema.parse("103.101.102.104") +const resultTow = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") + +console.log(resultOne, resultTow) +``` + +```javascript +// define a schema for string with ipv4 validator +const stringSchema = a.string().ipv4() + +// validate & parse the string +const resultOne = stringSchema.parse("103.101.102.104") +const resultTow = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") + +console.log(resultOne, resultTow) +``` + +::: + +**Output** + +```bash +{ value: '103.101.102.104' } + +{ + errors: [ + { + reason: 'Value must be a valid IPv4 address', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + } + ] +} +``` + +## `ipv6()` + +The `ipv6` validator checks if the string is a valid IPv6 address. + +::: code-group + +```typescript +// define a schema for string with ipv6 validator +const stringSchema = a.string().ipv6() + +// validate & parse the string +const resultOne = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") +const resultTwo = stringSchema.parse("103.101.102.104") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with ipv6 validator +const stringSchema = a.string().ipv6() + +// validate & parse the string +const resultOne = stringSchema.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334") +const resultTwo = stringSchema.parse("103.101.102.104") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334' } + +{ + errors: [ + { + reason: 'Value must be a valid IPv6 address', + value: '103.101.102.104', + } + ] +} +``` + +## `uuid()` + +The `uuid` validator checks if the string is a valid UUID. + +::: code-group + +```typescript +// define a schema for string with uuid validator +const stringSchema = a.string().uuid() + +// validate & parse the string +const resultOne = stringSchema.parse("550e8400-e29b-41d4-a716-446655440000") +const resultTwo = stringSchema.parse("550e8400-e29b-41d4-a716-44665544000") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with uuid validator +const stringSchema = a.string().uuid() + +// validate & parse the string +const resultOne = stringSchema.parse("550e8400-e29b-41d4-a716-446655440000") +const resultTwo = stringSchema.parse("550e8400-e29b-41d4-a716-44665544000") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '550e8400-e29b-41d4-a716-446655440000' } + +{ + errors: [ + { + reason: 'Value must be a valid UUID', + value: '550e8400-e29b-41d4-a716-44665544000', + } + ] +} +``` + +## `mongoid()` + +The `mongoid` validator checks if the string is a valid MongoDB ObjectId. + +::: code-group + +```typescript +// define a schema for string with mongoid validator +const stringSchema = a.string().mongoid() + +// validate & parse the string +const resultOne = stringSchema.parse("507f1f77bcf86cd799439011") +const resultTwo = stringSchema.parse("507f1f77bcf86cd79943901") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with mongoid validator +const stringSchema = a.string().mongoid() + +// validate & parse the string +const resultOne = stringSchema.parse("507f1f77bcf86cd799439011") +const resultTwo = stringSchema.parse("507f1f77bcf86cd79943901") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '507f1f77bcf86cd799439011' } + +{ + errors: [ + { + reason: 'Value must be a valid MongoDB ObjectId', + value: '507f1f77bcf86cd79943901', + } + ] +} +``` + +## `guid()` + +The `guid` validator checks if the string is a valid GUID. + +::: code-group + +```typescript +// define a schema for string with guid validator +const stringSchema = a.string().guid() + +// validate & parse the string +const resultOne = stringSchema.parse("550e8400-e29b-41d4-a716-446655440000") +const resultTwo = stringSchema.parse("550e8400-e29b-41d4-a716-44665544000") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with guid validator +const stringSchema = a.string().guid() + +// validate & parse the string +const resultOne = stringSchema.parse("550e8400-e29b-41d4-a716-446655440000") +const resultTwo = stringSchema.parse("550e8400-e29b-41d4-a716-44665544000") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '550e8400-e29b-41d4-a716-446655440000' } + +{ + errors: [ + { + reason: 'Value must be a valid GUID', + value: '550e8400-e29b-41d4-a716-44665544000', + } + ] +} +``` + +## `cuid()` + +The `cuid` validator checks if the string is a valid CUID. + +::: code-group + +```typescript +// define a schema for string with cuid validator +const stringSchema = a.string().cuid() + +// validate & parse the string +const resultOne = stringSchema.parse("ck7q2x1qg0000b3z1z1z6zj5z") +const resultTwo = stringSchema.parse("ck7q2x1qg0000b3z1z1z6zj5") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with cuid validator +const stringSchema = a.string().cuid() + +// validate & parse the string +const resultOne = stringSchema.parse("ck7q2x1qg0000b3z1z1z6zj5z") +const resultTwo = stringSchema.parse("ck7q2x1qg0000b3z1z1z6zj5") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'ck7q2x1qg0000b3z1z1z6zj5z' } + +{ + errors: [ + { + reason: 'Value must be a valid CUID', + value: 'ck7q2x1qg0000b3z1z1z6zj5', + } + ] +} +``` + +## `json()` + +The `json` validator checks if the string is a valid JSON string. + +::: code-group + +```typescript +// define a schema for string with json validator +const stringSchema = a.string().json() + +// validate & parse the string +const resultOne = stringSchema.parse('{"name": "John Doe"}') +const resultTwo = stringSchema.parse('{"name": "John Doe"') + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with json validator +const stringSchema = a.string().json() + +// validate & parse the string +const resultOne = stringSchema.parse('{"name": "John Doe"}') +const resultTwo = stringSchema.parse('{"name": "John Doe"') + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '{"name": "John Doe"}' } + +{ + errors: [ + { + reason: 'Value must be a valid JSON string', + value: '{"name": "John Doe"', + } + ] +} +``` + +## `mac()` + +The `mac` validator checks if the string is a valid MAC address. + +::: code-group + +```typescript +// define a schema for string with mac validator +const stringSchema = a.string().mac() + +// validate & parse the string +const resultOne = stringSchema.parse("00:0a:95:9d:68:16") +const resultTwo = stringSchema.parse("00:0a:95:9d:68:16:00") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with mac validator +const stringSchema = a.string().mac() + +// validate & parse the string +const resultOne = stringSchema.parse("00:0a:95:9d:68:16") +const resultTwo = stringSchema.parse("00:0a:95:9d:68:16:00") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '00:0a:95:9d:68:16' } + +{ + errors: [ + { + reason: 'Value must be a valid MAC address', + value: '00:0a:95:9d:68:16:00', + } + ] +} +``` + +## `creditcard()` + +The `creditcard` validator checks if the string is a valid credit card number. + +::: code-group + +```typescript +// define a schema for string with creditcard validator +const stringSchema = a.string().creditcard() + +// validate & parse the string +const resultOne = stringSchema.parse("4111111111111111") +const resultTwo = stringSchema.parse("4111111111111112") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with creditcard validator +const stringSchema = a.string().creditcard() + +// validate & parse the string +const resultOne = stringSchema.parse("4111111111111111") +const resultTwo = stringSchema.parse("4111111111111112") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '4111111111111111' } + +{ + errors: [ + { + reason: 'Value must be a valid credit card number', + value: '411111111111 + } + ] +} +``` + +## `country()` + +The `country` validator checks if the string is a valid country code. + +::: code-group + +```typescript +// define a schema for string with country validator +const stringSchema = a.string().country() + +// validate & parse the string +const resultOne = stringSchema.parse("BD") +const resultTwo = stringSchema.parse("USA") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with country validator +const stringSchema = a.string().country() + +// validate & parse the string +const resultOne = stringSchema.parse("BD") +const resultTwo = stringSchema.parse("USA") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'BD' } + +{ + errors: [ + { + reason: 'Value must be a valid country code', + value: 'USA', + } + ] +} +``` + +## `postalcode()` + +The `postalcode` validator checks if the string is a valid postal code. + +::: code-group + +```typescript +// define a schema for string with postalcode validator +const stringSchema = a.string().postalcode() + +// validate & parse the string +const resultOne = stringSchema.parse("1000") +const resultTwo = stringSchema.parse("1000a") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with postalcode validator +const stringSchema = a.string().postalcode() + +// validate & parse the string +const resultOne = stringSchema.parse("1000") +const resultTwo = stringSchema.parse("1000a") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '1000' } + +{ + errors: [ + { + reason: 'Value must be a valid postal code', + value: '1000a', + } + ] +} +``` + +## `passport()` + +The `passport` validator checks if the string is a valid passport number. + +::: code-group + +```typescript +// define a schema for string with passport validator +const stringSchema = a.string().passport() + +// validate & parse the string +const resultOne = stringSchema.parse("AB1234567") +const resultTwo = stringSchema.parse("A123456") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with passport validator +const stringSchema = a.string().passport() + +// validate & parse the string +const resultOne = stringSchema.parse("AB1234567") +const resultTwo = stringSchema.parse("A123456") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'AB1234567' } + +{ + errors: [ + { + reason: 'Value must be a valid passport number', + value: 'A123456', + } + ] +} +``` + +## `password()` + +The `password` validator checks if the string is a valid password. + +::: code-group + +```typescript +// define a schema for string with password validator +const stringSchema = a.string().password() + +// validate & parse the string +const resultOne = stringSchema.parse("P@ssw0rd") +const resultTwo = stringSchema.parse("password") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with password validator +const stringSchema = a.string().password() + +// validate & parse the string +const resultOne = stringSchema.parse("P@ssw0rd") +const resultTwo = stringSchema.parse("password") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'P@ssw0rd' } + +{ + errors: [ + { + reason: 'Value must be a valid password', + value: 'password', + } + ] +} +``` + +## `currency()` + +The `currency` validator checks if the string is a valid currency code. + +::: code-group + +```typescript +// define a schema for string with currency validator +const stringSchema = a.string().currency() + +// validate & parse the string +const resultOne = stringSchema.parse("USD") +const resultTwo = stringSchema.parse("US") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with currency validator +const stringSchema = a.string().currency() + +// validate & parse the string +const resultOne = stringSchema.parse("USD") +const resultTwo = stringSchema.parse("US") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'USD' } + +{ + errors: [ + { + reason: 'Value must be a valid currency code', + value: 'US', + } + ] +} +``` + +## `dataUri()` + +The `datauri` validator checks if the string is a valid data URI. + +::: code-group + +```typescript +// define a schema for string with datauri validator +const stringSchema = a.string().datauri() + +// validate & parse the string +const resultOne = stringSchema.parse( + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mNk" +) + +const resultTwo = stringSchema.parse( + "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8" +) + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with datauri validator +const stringSchema = a.string().datauri() + +// validate & parse the string +const resultOne = stringSchema.parse( + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mNk" +) + +const resultTwo = stringSchema.parse( + "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8" +) + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mNk' } + +{ + errors: [ + { + reason: 'Value must be a valid data URI', + value: 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8', + } + ] +} +``` + +## `mimeType()` + +The `mimetype` validator checks if the string is a valid MIME type. + +::: code-group + +```typescript +// define a schema for string with mimetype validator +const stringSchema = a.string().mimetype() + +// validate & parse the string +const resultOne = stringSchema.parse("image/png") +const resultTwo = stringSchema.parse("image") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with mimetype validator +const stringSchema = a.string().mimetype() + +// validate & parse the string +const resultOne = stringSchema.parse("image/png") +const resultTwo = stringSchema.parse("image") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'image/png' } + +{ + errors: [ + { + reason: 'Value must be a valid MIME type', + value: 'image', + } + ] +} +``` + +## `latlong()` + +The `latlong` validator checks if the string is a valid latitude and longitude. + +::: code-group + +```typescript +// define a schema for string with latlong validator +const stringSchema = a.string().latlong() + +// validate & parse the string +const resultOne = stringSchema.parse("23.8103,90.4125") +const resultTwo = stringSchema.parse("23.8103,90.4125,23.8103") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with latlong validator +const stringSchema = a.string().latlong() + +// validate & parse the string +const resultOne = stringSchema.parse("23.8103,90.4125") +const resultTwo = stringSchema.parse("23.8103,90.4125,23.8103") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '23.8103,90.4125' } + +{ + errors: [ + { + reason: 'Value must be a valid latitude and longitude', + value: '23.8103,90.4125,23.8103', + } + ] +} +``` + +## `hexColor()` + +The `hexcolor` validator checks if the string is a valid hexadecimal color. + +::: code-group + +```typescript +// define a schema for string with hexcolor validator +const stringSchema = a.string().hexColor() + +// validate & parse the string +const resultOne = stringSchema.parse("#ff0000") +const resultTwo = stringSchema.parse("#ff000") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with hexcolor validator +const stringSchema = a.string().hexColor() + +// validate & parse the string +const resultOne = stringSchema.parse("#ff0000") +const resultTwo = stringSchema.parse("#ff000") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '#ff0000' } + +{ + errors: [ + { + reason: 'Value must be a valid hexadecimal color', + value: '#ff000', + } + ] +} +``` + +## `rgbColor()` + +The `rgbcolor` validator checks if the string is a valid RGB color. + +::: code-group + +```typescript +// define a schema for string with rgbcolor validator +const stringSchema = a.string().rgbColor() + +// validate & parse the string +const resultOne = stringSchema.parse("rgb(255, 0, 0)") +const resultTwo = stringSchema.parse("rgb(255, 0, 0, 255)") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with rgbcolor validator +const stringSchema = a.string().rgbColor() + +// validate & parse the string +const resultOne = stringSchema.parse("rgb(255, 0, 0)") +const resultTwo = stringSchema.parse("rgb(255, 0, 0, 255)") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'rgb(255, 0, 0)' } + +{ + errors: [ + { + reason: 'Value must be a valid RGB color', + value: 'rgb(255, 0, 0, 255)', + } + ] +} +``` + +## `hslColor()` + +The `hslcolor` validator checks if the string is a valid HSL color. + +::: code-group + +```typescript +// define a schema for string with hslcolor validator +const stringSchema = a.string().hslColor() + +// validate & parse the string +const resultOne = stringSchema.parse("hsl(0, 100%, 50%)") +const resultTwo = stringSchema.parse("hsl(0, 100%, 50%, 1)") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with hslcolor validator +const stringSchema = a.string().hslColor() + +// validate & parse the string +const resultOne = stringSchema.parse("hsl(0, 100%, 50%)") +const resultTwo = stringSchema.parse("hsl(0, 100%, 50%, 1)") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'hsl(0, 100%, 50%)' } + +{ + errors: [ + { + reason: 'Value must be a valid HSL color', + value: 'hsl(0, 100%, 50%, 1)', + } + ] +} +``` + +## `semver()` + +The `semver` validator checks if the string is a valid semantic version. + +::: code-group + +```typescript +// define a schema for string with semver validator +const stringSchema = a.string().semver() + +// validate & parse the string +const resultOne = stringSchema.parse("1.2.3") +const resultTwo = stringSchema.parse("1.2") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with semver validator +const stringSchema = a.string().semver() + +// validate & parse the string +const resultOne = stringSchema.parse("1.2.3") +const resultTwo = stringSchema.parse("1.2") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '1.2.3' } + +{ + errors: [ + { + reason: 'Value must be a valid semantic version', + value: '1.2', + } + ] +} +``` + +## `jwt()` + +The `jwt` validator checks if the string is a valid JSON Web Token. + +::: code-group + +```typescript +// define a schema for string with jwt validator +const stringSchema = a.string().jwt() + +// validate & parse the string +const resultOne = stringSchema.parse( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" +) + +const resultTwo = stringSchema.parse( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ" +) + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with jwt validator +const stringSchema = a.string().jwt() + +// validate & parse the string +const resultOne = stringSchema.parse( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpv" +) + +const resultTwo = stringSchema.parse( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpv" +) + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6JkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' } + +{ + errors: [ + { + reason: 'Value must be a valid JSON Web Token', + value: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6JkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', + } + ] +} +``` + +## `md5()` + +The `md5` validator checks if the string is a valid MD5 hash. + +::: code-group + +```typescript +// define a schema for string with md5 validator +const md5Schema = a.string().md5() + +// validate & parse the string +const resultOne = md5Schema.parse("d41d8cd98f00b204e9800998ecf8427e") +const resultTwo = md5Schema.parse("d41d8cd98f00b204e9800998ecf8427") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with md5 validator +const md5Schema = a.string().md5() + +// validate & parse the string +const resultOne = md5Schema.parse("d41d8cd98f00b204e9800998ecf8427e") +const resultTwo = md5Schema.parse("d41d8cd98f00b204e9800998ecf8427") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'd41d8cd98f00b204e9800998ecf8427e' } + +{ + errors: [ + { + reason: 'Value must be a valid MD5 hash', + value: 'd41d8cd98f00b204e9800998ecf8427', + } + ] +} +``` + +## `sha1()` + +The `sha1` validator checks if the string is a valid SHA1 hash. + +::: code-group + +```typescript +// define a schema for string with sha1 validator +const sha1Schema = a.string().sha1() + +// validate & parse the string +const resultOne = sha1Schema.parse("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") +const resultTwo = sha1Schema.parse("2fd4e1c67a2d28fced849ee1bb76e7391b93eb1") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with sha1 validator +const sha1Schema = a.string().sha1() + +// validate & parse the string +const resultOne = sha1Schema.parse("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") +const resultTwo = sha1Schema.parse("2fd4e1c67a2d28fced849ee1bb76e7391b93eb1") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12' } + +{ + errors: [ + { + reason: 'Value must be a valid SHA1 hash', + value: '2fd4e1c67a2d28fced849ee1bb76e7391b93eb1', + } + ] +} + +``` + +## `sha256()` + +The `sha256` validator checks if the string is a valid SHA256 hash. + +::: code-group + +```typescript +// define a schema for string with sha256 validator +const sha256Schema = a.string().sha256() + +// validate & parse the string +const resultOne = sha256Schema.parse( + "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e" +) + +const resultTwo = sha256Schema.parse( + "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146" +) + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with sha256 validator +const sha256Schema = a.string().sha256() + +// validate & parse the string +const resultOne = sha256Schema.parse( + "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e" +) + +const resultTwo = sha256Schema.parse( + "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146" +) + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e' } + +{ + errors: [ + { + reason: 'Value must be a valid SHA256 hash', + value: 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146', + } + ] +} + +``` + +## `sha512()` + +The `sha512` validator checks if the string is a valid SHA512 hash. + +::: code-group + +```typescript +// define a schema for string with sha512 validator +const sha512Schema = a.string().sha512() + +// validate & parse the string +const resultOne = sha512Schema.parse( + "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae493" +) + +const resultTwo = sha512Schema.parse( + "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae49" +) + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with sha512 validator +const sha512Schema = a.string().sha512() + +// validate & parse the string +const resultOne = sha512Schema.parse( + "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae493" +) + +const resultTwo = sha512Schema.parse( + "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae49" +) + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae493' } + +{ + errors: [ + { + reason: 'Value must be a valid SHA512 hash', + value: '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae49', + } + ] +} + +``` + +## `iban()` + +The `iban` validator checks if the string is a valid International Bank Account Number (IBAN). + +::: code-group + +```typescript +// define a schema for string with iban validator +const ibanSchema = a.string().iban() + +// validate & parse the string +const resultOne = ibanSchema.parse("GB82WEST12345698765432") +const resultTwo = ibanSchema.parse("GB82WEST1234569876543") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with iban validator +const ibanSchema = a.string().iban() + +// validate & parse the string +const resultOne = ibanSchema.parse("GB82WEST12345698765432") +const resultTwo = ibanSchema.parse("GB82WEST1234569876543") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'GB82WEST12345698765432' } + +{ + errors: [ + { + reason: 'Value must be a valid IBAN', + value: 'GB82WEST1234569876543', + } + ] +} +``` + +## `bic()` + +The `bic` validator checks if the string is a valid Bank Identifier Code (BIC). + +::: code-group + +```typescript +// define a schema for string with bic validator +const bicSchema = a.string().bic() + +// validate & parse the string +const resultOne = bicSchema.parse("DEUTDEFF500") +const resultTwo = bicSchema.parse("DEUTDEFF50") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with bic validator +const bicSchema = a.string().bic() + +// validate & parse the string +const resultOne = bicSchema.parse("DEUTDEFF500") +const resultTwo = bicSchema.parse("DEUTDEFF50") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'DEUTDEFF500' } + +{ + errors: [ + { + reason: 'Value must be a valid BIC', + value: 'DEUTDEFF50', + } + ] +} +``` + +## `isbn()` + +The `isbn` validator checks if the string is a valid International Standard Book Number (ISBN). + +::: code-group + +```typescript +// define a schema for string with isbn validator +const isbnSchema = a.string().isbn() + +// validate & parse the string +const resultOne = isbnSchema.parse("978-3-16-148410-0") +const resultTwo = isbnSchema.parse("978-3-16-148410") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with isbn validator +const isbnSchema = a.string().isbn() + +// validate & parse the string +const resultOne = isbnSchema.parse("978-3-16-148410-0") +const resultTwo = isbnSchema.parse("978-3-16-148410") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '978-3-16-148410-0' } + +{ + errors: [ + { + reason: 'Value must be a valid ISBN', + value: '978-3-16-148410', + } + ] +} +``` + +## `locale()` + +The `locale` validator checks if the string is a valid locale code. + +::: code-group + +```typescript +// define a schema for string with locale validator +const localeSchema = a.string().locale() + +// validate & parse the string +const resultOne = localeSchema.parse("en-US") +const resultTwo = localeSchema.parse("en") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with locale validator +const localeSchema = a.string().locale() + +// validate & parse the string +const resultOne = localeSchema.parse("en-US") +const resultTwo = localeSchema.parse("en") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'en-US' } + +{ + errors: [ + { + reason: 'Value must be a valid locale code', + value: 'en', + } + ] +} +``` + +## `otp()` + +The `otp` validator checks if the string is a valid One-Time Password (OTP). + +::: code-group + +```typescript +// define a schema for string with otp validator +const otpSchema = a.string().otp() + +// validate & parse the string +const resultOne = otpSchema.parse("123456") +const resultTwo = otpSchema.parse("12345") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with otp validator +const otpSchema = a.string().otp() + +// validate & parse the string +const resultOne = otpSchema.parse("123456") +const resultTwo = otpSchema.parse("12345") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '123456' } + +{ + errors: [ + { + reason: 'Value must be a valid OTP', + value: '12345', + } + ] +} +``` + +## `slug()` + +The `slug` validator checks if the string is a valid slug. + +::: code-group + +```typescript +// define a schema for string with slug validator +const slugSchema = a.string().slug() + +// validate & parse the string +const resultOne = slugSchema.parse("hello-world") +const resultTwo = slugSchema.parse("hello world") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with slug validator +const slugSchema = a.string().slug() + +// validate & parse the string +const resultOne = slugSchema.parse("hello-world") +const resultTwo = slugSchema.parse("hello world") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'hello-world' } + +{ + errors: [ + { + reason: 'Value must be a valid slug', + value: 'hello world', + } + ] +} +``` + +## `fqdn()` + +The `fqdn` validator checks if the string is a valid Fully Qualified Domain Name (FQDN). + +::: code-group + +```typescript +// define a schema for string with fqdn validator +const fqdnSchema = a.string().fqdn() + +// validate & parse the string +const resultOne = fqdnSchema.parse("example.com") +const resultTwo = fqdnSchema.parse("example") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with fqdn validator +const fqdnSchema = a.string().fqdn() + +// validate & parse the string +const resultOne = fqdnSchema.parse("example.com") +const resultTwo = fqdnSchema.parse("example") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'example.com' } + +{ + errors: [ + { + reason: 'Value must be a valid FQDN', + value: 'example', + } + ] +} +``` + +## `variableName()` + +The `variablename` validator checks if the string is a valid variable name. + +::: code-group + +```typescript +// define a schema for string with variablename validator +const variableNameSchema = a.string().variablename() + +// validate & parse the string +const resultOne = variableNameSchema.parse("variableName") +const resultTwo = variableNameSchema.parse("variable Name") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with variablename validator +const variableNameSchema = a.string().variablename() + +// validate & parse the string +const resultOne = variableNameSchema.parse("variableName") +const resultTwo = variableNameSchema.parse("variable Name") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'variableName' } + +{ + errors: [ + { + reason: 'Value must be a valid variable name', + value: 'variable Name', + } + ] +} +``` + +## `date()` + +The `date` validator checks if the string is a valid date. + +::: code-group + +```typescript +// define a schema for string with date validator +const dateSchema = a.string().date() + +// validate & parse the string +const resultOne = dateSchema.parse("2021-12-31") +const resultTwo = dateSchema.parse("2021-12-32") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with date validator +const dateSchema = a.string().date() + +// validate & parse the string +const resultOne = dateSchema.parse("2021-12-31") +const resultTwo = dateSchema.parse("2021-12-32") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '2021-12-31' } + +{ + errors: [ + { + reason: 'Value must be a valid date', + value: '2021-12-32', + } + ] +} +``` + +## `time()` + +The `time` validator checks if the string is a valid time. + +::: code-group + +```typescript +// define a schema for string with time validator +const timeSchema = a.string().time() + +// validate & parse the string +const resultOne = timeSchema.parse("12:00") +const resultTwo = timeSchema.parse("25:00") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with time validator +const timeSchema = a.string().time() + +// validate & parse the string +const resultOne = timeSchema.parse("12:00") +const resultTwo = timeSchema.parse("25:00") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '12:00' } + +{ + errors: [ + { + reason: 'Value must be a valid time', + value: '25:00', + } + ] +} +``` + +## `minutesOrSeconds()` + +The `minutesorseconds` validator checks if the string is a valid minutes or seconds. + +::: code-group + +```typescript +// define a schema for string with minutesorseconds validator +const minutesOrSecondsSchema = a.string().minutesorseconds() + +// validate & parse the string +const resultOne = minutesOrSecondsSchema.parse("12") +const resultTwo = minutesOrSecondsSchema.parse("63") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with minutesorseconds validator +const minutesOrSecondsSchema = a.string().minutesorseconds() + +// validate & parse the string +const resultOne = minutesOrSecondsSchema.parse("12") +const resultTwo = minutesOrSecondsSchema.parse("63") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '12' } + +{ + errors: [ + { + reason: 'Value must be a valid minutes or seconds', + value: '63', + } + ] +} +``` + +## `hours()` + +The `hours` validator checks if the string is a valid hours. + +::: code-group + +```typescript +// define a schema for string with hours validator +const hoursSchema = a.string().hours() + +// validate & parse the string +const resultOne = hoursSchema.parse("12") +const resultTwo = hoursSchema.parse("25") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with hours validator +const hoursSchema = a.string().hours() + +// validate & parse the string +const resultOne = hoursSchema.parse("12") +const resultTwo = hoursSchema.parse("25") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: '12' } + +{ + errors: [ + { + reason: 'Value must be a valid hours', + value: '25', + } + ] +} +``` + +## `weekday()` + +The `weekday` validator checks if the string is a valid weekday. + +::: code-group + +```typescript +// define a schema for string with weekday validator +const weekdaySchema = a.string().weekday() + +// validate & parse the string +const resultOne = weekdaySchema.parse("monday") +const resultTwo = weekdaySchema.parse("Mon") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with weekday validator +const weekdaySchema = a.string().weekday() + +// validate & parse the string +const resultOne = weekdaySchema.parse("monday") +const resultTwo = weekdaySchema.parse("Mon") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'monday' } + +{ + errors: [ + { + reason: 'Value must be a valid weekday', + value: 'Mon', + } + ] +} +``` + +## `timezone()` + +The `timezone` validator checks if the string is a valid timezone. + +::: code-group + +```typescript +// define a schema for string with timezone validator +const timezoneSchema = a.string().timezone() + +// validate & parse the string +const resultOne = timezoneSchema.parse("Asia/Dhaka") +const resultTwo = timezoneSchema.parse("Asia") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with timezone validator + +const timezoneSchema = a.string().timezone() + +// validate & parse the string +const resultOne = timezoneSchema.parse("Asia/Dhaka") +const resultTwo = timezoneSchema.parse("Asia") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'Asia/Dhaka' } + +{ + errors: [ + { + reason: 'Value must be a valid timezone', + value: 'Asia', + } + ] +} +``` + +## `base()` + +The `base` validator checks if the string is a valid base. + +::: code-group + +```typescript +// define a schema for string with base validator +const baseSchema = a.string().base() + +// validate & parse the string +const resultOne = baseSchema.parse("d41d8cd98f00b204e9800998ecf8427e") +const resultTwo = baseSchema.parse("d41d8cd98f00b204e9800998ecf8427") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with base validator +const baseSchema = a.string().base() + +// validate & parse the string +const resultOne = baseSchema.parse("d41d8cd98f00b204e9800998ecf8427e") +const resultTwo = baseSchema.parse("d41d8cd98f00b204e9800998ecf8427") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'd41d8cd98f00b204e9800998ecf8427e' } + +{ + errors: [ + { + reason: 'Value must be a valid base', + value: 'd41d8cd98f00b204e9800998ecf8427', + } + ] +} +``` + +## `contains()` + +The `contains` validator checks if the string contains a specific substring. + +::: code-group + +```typescript +// define a schema for string with contains validator +const containsSchema = a.string().contains("hello") + +// validate & parse the string +const resultOne = containsSchema.parse("hello world") +const resultTwo = containsSchema.parse("world") + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with contains validator +const containsSchema = a.string().contains("hello") + +// validate & parse the string +const resultOne = containsSchema.parse("hello world") +const resultTwo = containsSchema.parse("world") + +console.log(resultOne, resultTwo) +``` + +::: + +**Output** + +```bash +{ value: 'hello world' } + +{ + errors: [ + { + reason: 'Value must contain "hello"', + value: 'world', + } + ] +} +``` + +## `magnetUri()` + +The `magneturi` validator checks if the string is a valid Magnet URI. + +::: code-group + +```typescript +// define a schema for string with magneturi validator +const magnetUriSchema = a.string().magneturi() + +// validate & parse the string +const resultOne = magnetUriSchema.parse( + "magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f9d4f9" +) +const resultTwo = magnetUriSchema.parse( + "magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f01234" +) + +console.log(resultOne, resultTwo) +``` + +```javascript +// define a schema for string with magneturi validator +const magnetUriSchema = a.string().magneturi() + +// validate & parse the string +const resultOne = magnetUriSchema.parse( + "magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f9d4f9" +) +const resultTwo = magnetUriSchema.parse( + "magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f01234" +) + +console.log(resultOne, resultTwo) +``` + ::: + +**Output** + +```bash +{ value: 'magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f9d4f9' } + +{ + errors: [ + { + reason: 'Value must be a valid Magnet URI', + value: 'magnet:?xt=urn:btih:1220a7b4e4e0f4f9d4f9d4f9d4f9d4f9d4f01234', + } + ] +} +``` + +## Conclusion + +This is the end of the string schema documentation. We have covered all the available validators and the default behavior of the string schema. You can raise an issue on the [GitHub repository](https://github.com/mahabubx7/akar) if you have any questions or suggestions or discovered bugs. diff --git a/lib/schemas/array.ts b/lib/schemas/array.ts index f90249d..44418b0 100644 --- a/lib/schemas/array.ts +++ b/lib/schemas/array.ts @@ -6,21 +6,46 @@ * @license MIT */ -import { isArray } from "../validators/array" +import { arrayChecker } from "../validators" import { AkarBase } from "./base" export class AkarArray extends AkarBase { + private miniumLength?: number + private maximumLength?: number + private hasRange: [number, number] = [0, -1] + private isUnique?: boolean + constructor(private schema: AkarBase) { super() } + min(min: number): this { + this.miniumLength = min + return this + } + + max(max: number): this { + this.maximumLength = max + return this + } + + range(min: number, max: number): this { + this.hasRange = [min, max] + return this + } + + unique(): this { + this.isUnique = true + return this + } + parse(input: any): { value?: T[] errors?: { field: string; reason: string; value?: any }[] } { const errors: { field: string; reason: string; value?: any }[] = [] - if (!isArray(input)) { + if (!arrayChecker.isArray(input)) { errors.push({ field: "array", reason: "Invalid type, expected array", @@ -29,6 +54,36 @@ export class AkarArray extends AkarBase { return { errors } } + if ( + this.miniumLength && + !arrayChecker.minLength(input, this.miniumLength) + ) { + errors.push({ + field: "array", + reason: `Expected at least ${this.miniumLength} items`, + value: input + }) + } + + if ( + this.maximumLength && + !arrayChecker.maxLength(input, this.maximumLength) + ) { + errors.push({ + field: "array", + reason: `Expected at most ${this.maximumLength} items`, + value: input + }) + } + + if (this.isUnique && !arrayChecker.isUnique(input)) { + errors.push({ + field: "array", + reason: "Expected unique items", + value: input + }) + } + const result: T[] = [] input.forEach((item, index) => { diff --git a/lib/schemas/object.ts b/lib/schemas/object.ts index df6b3e4..9651cf8 100644 --- a/lib/schemas/object.ts +++ b/lib/schemas/object.ts @@ -14,8 +14,6 @@ import { AkarBase } from "./base" export class AkarObject> extends AkarBase { private shape: { [K in keyof T]: AkarBase } private defaults: Partial - private hasOwnKeys: string[] | null = null - private hasOwnValues: unknown[] | null = null private isJson: boolean = false private compare: T | null = null private isEqual: boolean = false @@ -31,24 +29,6 @@ export class AkarObject> extends AkarBase { this.defaults = defaults } - hasKeys(keys: string[]): this { - if (!this.hasOwnKeys) { - this.hasOwnKeys = keys - return this - } - this.hasOwnKeys = [...this.hasOwnKeys!, ...keys] - return this - } - - hasValues(values: unknown[]): this { - if (!this.hasOwnValues) { - this.hasOwnValues = values - return this - } - this.hasOwnValues = [...this.hasOwnValues!, ...values] - return this - } - equalTo(compare: T): this { this.isEqual = true this.compare = compare @@ -110,7 +90,7 @@ export class AkarObject> extends AkarBase { if (this.isDeepEqual && !objectChecker.isDeepEqual(input, this.compare!)) { errors.push({ field: "object", - reason: `Object must be deeply equal to ${JSON.stringify(this.compare)}`, + reason: `Object must be deeply equal!`, value: input }) } @@ -121,7 +101,7 @@ export class AkarObject> extends AkarBase { ) { errors.push({ field: "object", - reason: `Object must be shallowly equal to ${JSON.stringify(this.compare)}`, + reason: `Object must be shallowly equal!`, value: input }) } diff --git a/lib/validators/__tests__/array.test.ts b/lib/validators/__tests__/array.test.ts index 6796a13..a0d78c1 100644 --- a/lib/validators/__tests__/array.test.ts +++ b/lib/validators/__tests__/array.test.ts @@ -11,18 +11,6 @@ describe("Array validators", () => { expect(arr.isArray(undefined)).toBe(false) }) - test("isEmpty", () => { - expect(arr.isEmpty([])).toBe(true) - expect(arr.isEmpty([1])).toBe(false) - expect(arr.isEmpty([1, 2])).toBe(false) - }) - - test("isNotEmpty", () => { - expect(arr.isNotEmpty([])).toBe(false) - expect(arr.isNotEmpty([1])).toBe(true) - expect(arr.isNotEmpty([1, 2])).toBe(true) - }) - test("length", () => { expect(arr.length([], 0)).toBe(true) expect(arr.length([1], 1)).toBe(true) diff --git a/lib/validators/__tests__/string.test.ts b/lib/validators/__tests__/string.test.ts index b1f3ee1..a6cb223 100644 --- a/lib/validators/__tests__/string.test.ts +++ b/lib/validators/__tests__/string.test.ts @@ -331,9 +331,9 @@ describe("String validators", () => { expect(str.isWeekday("8")).toBe(false) expect(str.isWeekday("0")).toBe(false) expect(str.isWeekday("7")).toBe(false) - expect(str.isWeekday("mon")).toBe(true) - expect(str.isWeekday("tue")).toBe(true) - expect(str.isWeekday("wed")).toBe(true) + expect(str.isWeekday("monday")).toBe(true) + expect(str.isWeekday("tuesday")).toBe(true) + expect(str.isWeekday("wednesday")).toBe(true) }) test("isSlug", () => { diff --git a/lib/validators/array.ts b/lib/validators/array.ts index 131156d..f39914d 100644 --- a/lib/validators/array.ts +++ b/lib/validators/array.ts @@ -10,10 +10,10 @@ export const isArray = (input: unknown): input is T[] => Array.isArray(input) // Empty: check if the array is empty -export const isEmpty = (input: T[]): boolean => input.length === 0 +// export const isEmpty = (input: T[]): boolean => input.length === 0 -// Not Empty: check if the array is not empty -export const isNotEmpty = (input: T[]): boolean => input.length > 0 +// // Not Empty: check if the array is not empty +// export const isNotEmpty = (input: T[]): boolean => input.length > 0 // Length: check if the array length is equal to the expected length export const length = (input: T[], expected: number): boolean => diff --git a/lib/validators/string.ts b/lib/validators/string.ts index 0f1b962..c755ed3 100644 --- a/lib/validators/string.ts +++ b/lib/validators/string.ts @@ -412,11 +412,21 @@ export const isHour = (input: string): boolean => // Minute or Seconds: check if the string is a valid minute export const isMinuteOrSeconds = (input: string): boolean => - /^[0-5][0-9]$/.test(input) + /^[0-6][0-9]$/.test(input) && Number(input) <= 60 // Weekday: check if the string is a valid weekday -export const isWeekday = (input: string): boolean => - /^(mon|tue|wed|thu|fri|sat|sun)$/i.test(input.toLowerCase()) +export const isWeekday = (input: string): boolean => { + const weekdays = [ + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + "sunday" + ] + return weekdays.includes(input.toLowerCase()) +} // Base: check if the string is a valid base export const isBase = (input: string, base: number): boolean =>