-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds rules for text, mediumText, and tinyText (#1520)
- Loading branch information
Showing
11 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as assert from 'assert' | ||
import { InvalidMediumTextError, MediumText, mediumText } from '../../rulr' | ||
|
||
test('mediumText should not allow invalid string input', () => { | ||
const input = 0 | ||
assert.throws(() => mediumText(input), InvalidMediumTextError) | ||
}) | ||
|
||
test('mediumText should allow valid text with 0 characters', () => { | ||
const input = '' | ||
const output: MediumText = mediumText(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('mediumText should allow valid text with 16,777,215 characters', () => { | ||
const input = Array(16777215).fill('a').join('') | ||
const output: MediumText = mediumText(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('mediumText should not allow invalid text with more than 16,777,215 characters', () => { | ||
const input = Array(16777216).fill('a').join('') | ||
assert.throws(() => mediumText(input), InvalidMediumTextError) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BaseError } from 'make-error' | ||
import { isString } from '../../valueRules/string/string' | ||
import { Constrained } from '../../core' | ||
|
||
export class InvalidMediumTextError extends BaseError { | ||
constructor() { | ||
super('expected no more than 255 characters') | ||
} | ||
} | ||
|
||
export const mediumTextSymbol = Symbol() | ||
|
||
export type MediumText = Constrained<typeof mediumTextSymbol, string> | ||
|
||
export function isMediumText(input: unknown): input is MediumText { | ||
return isString(input) && input.length <= 16777215 | ||
} | ||
|
||
export function mediumText(input: unknown) { | ||
if (isMediumText(input)) { | ||
return input | ||
} | ||
throw new InvalidMediumTextError() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# mediumText | ||
|
||
[Back to root readme.md](../../../readme.md) | ||
|
||
This function uses the `rulr.mediumText` guard to check the input is a valid medium text string ([max 16,777,215 characters](https://www.w3schools.com/sql/sql_datatypes.asp)) as shown in the example below. It should only throw `rulr.InvalidMediumTextError`. | ||
|
||
```ts | ||
import * as rulr from 'rulr' | ||
|
||
const constrainToExample = rulr.object({ | ||
required: { | ||
example: rulr.mediumText, | ||
}, | ||
}) | ||
|
||
type Example = rulr.Static<typeof constrainToExample> | ||
// { | ||
// example: rulr.MediumText | ||
// } | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: '', | ||
}) | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: Array(16777215).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Too many characters | ||
const example2: Example = constrainToExample({ | ||
example: Array(16777216).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Not a string | ||
const example3: Example = constrainToExample({ | ||
example: 1, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# text | ||
|
||
[Back to root readme.md](../../../readme.md) | ||
|
||
This function uses the `rulr.text` guard to check the input is a valid text string ([max 65,535 characters](https://www.w3schools.com/sql/sql_datatypes.asp)) as shown in the example below. It should only throw `rulr.InvalidTextError`. | ||
|
||
```ts | ||
import * as rulr from 'rulr' | ||
|
||
const constrainToExample = rulr.object({ | ||
required: { | ||
example: rulr.text, | ||
}, | ||
}) | ||
|
||
type Example = rulr.Static<typeof constrainToExample> | ||
// { | ||
// example: rulr.Text | ||
// } | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: '', | ||
}) | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: Array(65535).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Too many characters | ||
const example2: Example = constrainToExample({ | ||
example: Array(65536).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Not a string | ||
const example3: Example = constrainToExample({ | ||
example: 1, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as assert from 'assert' | ||
import { InvalidTextError, Text, text } from '../../rulr' | ||
|
||
test('text should not allow invalid string input', () => { | ||
const input = 0 | ||
assert.throws(() => text(input), InvalidTextError) | ||
}) | ||
|
||
test('text should allow valid text with 0 characters', () => { | ||
const input = '' | ||
const output: Text = text(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('text should allow valid text with 65,535 characters', () => { | ||
const input = Array(65535).fill('a').join('') | ||
const output: Text = text(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('text should not allow invalid text with more than 65,535 characters', () => { | ||
const input = Array(65536).fill('a').join('') | ||
assert.throws(() => text(input), InvalidTextError) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BaseError } from 'make-error' | ||
import { isString } from '../../valueRules/string/string' | ||
import { Constrained } from '../../core' | ||
|
||
export class InvalidTextError extends BaseError { | ||
constructor() { | ||
super('expected no more than 255 characters') | ||
} | ||
} | ||
|
||
export const textSymbol = Symbol() | ||
|
||
export type Text = Constrained<typeof textSymbol, string> | ||
|
||
export function isText(input: unknown): input is Text { | ||
return isString(input) && input.length <= 65535 | ||
} | ||
|
||
export function text(input: unknown) { | ||
if (isText(input)) { | ||
return input | ||
} | ||
throw new InvalidTextError() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# tinyText | ||
|
||
[Back to root readme.md](../../../readme.md) | ||
|
||
This function uses the `rulr.tinyText` guard to check the input is a valid tiny text string ([max 255 characters](https://www.w3schools.com/sql/sql_datatypes.asp)) as shown in the example below. It should only throw `rulr.InvalidTinyTextError`. | ||
|
||
```ts | ||
import * as rulr from 'rulr' | ||
|
||
const constrainToExample = rulr.object({ | ||
required: { | ||
example: rulr.tinyText, | ||
}, | ||
}) | ||
|
||
type Example = rulr.Static<typeof constrainToExample> | ||
// { | ||
// example: rulr.TinyText | ||
// } | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: '', | ||
}) | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: Array(255).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Too many characters | ||
const example2: Example = constrainToExample({ | ||
example: Array(256).fill('a').join(''), | ||
}) | ||
|
||
// Invalid: Not a string | ||
const example3: Example = constrainToExample({ | ||
example: 1, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as assert from 'assert' | ||
import { InvalidTinyTextError, TinyText, tinyText } from '../../rulr' | ||
|
||
test('tinyText should not allow invalid string input', () => { | ||
const input = 0 | ||
assert.throws(() => tinyText(input), InvalidTinyTextError) | ||
}) | ||
|
||
test('tinyText should allow valid text with 0 characters', () => { | ||
const input = '' | ||
const output: TinyText = tinyText(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('tinyText should allow valid text with 255 characters', () => { | ||
const input = Array(255).fill('a').join('') | ||
const output: TinyText = tinyText(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('tinyText should not allow invalid text with more than 255 characters', () => { | ||
const input = Array(256).fill('a').join('') | ||
assert.throws(() => tinyText(input), InvalidTinyTextError) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BaseError } from 'make-error' | ||
import { isString } from '../../valueRules/string/string' | ||
import { Constrained } from '../../core' | ||
|
||
export class InvalidTinyTextError extends BaseError { | ||
constructor() { | ||
super('expected no more than 255 characters') | ||
} | ||
} | ||
|
||
export const tinyTextSymbol = Symbol() | ||
|
||
export type TinyText = Constrained<typeof tinyTextSymbol, string> | ||
|
||
export function isTinyText(input: unknown): input is TinyText { | ||
return isString(input) && input.length <= 255 | ||
} | ||
|
||
export function tinyText(input: unknown) { | ||
if (isTinyText(input)) { | ||
return input | ||
} | ||
throw new InvalidTinyTextError() | ||
} |