@typetron/framework 0.5.30 → 0.5.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +31 -0
- package/CLAUDE.md +1 -0
- package/Cache/Readme.md +63 -0
- package/Container/Readme.md +95 -0
- package/Database/Readme.md +139 -0
- package/Encryption/Readme.md +54 -0
- package/Forms/Readme.md +79 -0
- package/Framework/Readme.md +138 -0
- package/Frontend/Angular/Readme.md +68 -0
- package/Frontend/Websockets/Readme.md +96 -0
- package/Mail/Readme.md +99 -0
- package/Models/Readme.md +85 -0
- package/Router/Readme.md +145 -0
- package/Storage/Readme.md +83 -0
- package/Support/Readme.md +71 -0
- package/Testing/Readme.md +80 -0
- package/Validation/Readme.md +91 -0
- package/package.json +20 -2
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Validation
|
|
2
|
+
|
|
3
|
+
- [Introduction](#introduction)
|
|
4
|
+
- [Using Rules](#using-rules)
|
|
5
|
+
- [Available Rules](#available-rules)
|
|
6
|
+
- [Optional Values](#optional-values)
|
|
7
|
+
- [Validating Files](#validating-files)
|
|
8
|
+
- [Custom Rules](#custom-rules)
|
|
9
|
+
|
|
10
|
+
## Introduction
|
|
11
|
+
|
|
12
|
+
The Validation module provides small rule classes for checking input values. Forms compose these rules and collect their messages, while
|
|
13
|
+
individual rules may also be used directly.
|
|
14
|
+
|
|
15
|
+
## Using Rules
|
|
16
|
+
|
|
17
|
+
Rule factories return classes. Construct the returned class when validating without a form:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Email } from '@Typetron/Validation'
|
|
21
|
+
|
|
22
|
+
const EmailRule = Email('Please provide a valid email address')
|
|
23
|
+
const rule = new EmailRule()
|
|
24
|
+
|
|
25
|
+
if (!rule.passes('email', 'john@example.com')) {
|
|
26
|
+
console.log(rule.message('email', 'john@example.com'))
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
When a message is not supplied, each built-in rule returns its default message.
|
|
31
|
+
|
|
32
|
+
## Available Rules
|
|
33
|
+
|
|
34
|
+
Typetron currently includes `Required`, `Email`, `MinLength`, `MaxLength`, `Optional`, and `FileExtension`. Parameterized rules are created
|
|
35
|
+
before use:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { MaxLength, MinLength } from '@Typetron/Validation'
|
|
39
|
+
|
|
40
|
+
const shortName = new (MinLength(3))()
|
|
41
|
+
const limitedName = new (MaxLength(50))()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Forms accept both direct factories such as `Required` and configured factories such as `MinLength(3)` through `@Rules`.
|
|
45
|
+
|
|
46
|
+
## Optional Values
|
|
47
|
+
|
|
48
|
+
`Optional` has special meaning when used by a form. If the value is `undefined` or `null`, the form skips the field's remaining rules. Empty
|
|
49
|
+
strings are not considered missing.
|
|
50
|
+
|
|
51
|
+
## Validating Files
|
|
52
|
+
|
|
53
|
+
`FileExtension` accepts a list of extensions without leading dots:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { File } from '@Typetron/Storage'
|
|
57
|
+
import { FileExtension } from '@Typetron/Validation'
|
|
58
|
+
|
|
59
|
+
const rule = new (FileExtension(['jpg', 'png']))()
|
|
60
|
+
const valid = rule.passes('avatar', new File('avatar.png'))
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The rule returns `false` when the value has no extension or is not a `File`-shaped value.
|
|
64
|
+
|
|
65
|
+
## Custom Rules
|
|
66
|
+
|
|
67
|
+
Extend `Rule` and implement an identifier, `passes`, and `message`:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { Rule } from '@Typetron/Validation'
|
|
71
|
+
|
|
72
|
+
class EvenNumber extends Rule {
|
|
73
|
+
identifier = 'evenNumber'
|
|
74
|
+
|
|
75
|
+
passes(
|
|
76
|
+
attribute: string,
|
|
77
|
+
value: string | number | object | boolean | undefined,
|
|
78
|
+
) {
|
|
79
|
+
return typeof value === 'number' && value % 2 === 0
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
message(
|
|
83
|
+
attribute: string,
|
|
84
|
+
value: string | number | object | boolean | undefined,
|
|
85
|
+
) {
|
|
86
|
+
return `The ${attribute} must be even`
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The identifier becomes the key for that rule in a form's error object.
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@typetron/framework",
|
|
3
3
|
"description": "Modern Node.js framework for modern apps written in Typescript",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.5.
|
|
5
|
+
"version": "0.5.31",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typetron",
|
|
8
8
|
"framework",
|
|
@@ -73,6 +73,24 @@
|
|
|
73
73
|
"url": "https://github.com/typetron/framework.git"
|
|
74
74
|
},
|
|
75
75
|
"files": [
|
|
76
|
-
"build"
|
|
76
|
+
"build",
|
|
77
|
+
"AGENTS.md",
|
|
78
|
+
"CLAUDE.md",
|
|
79
|
+
"Readme.md",
|
|
80
|
+
"Cache/Readme.md",
|
|
81
|
+
"Container/Readme.md",
|
|
82
|
+
"Database/Readme.md",
|
|
83
|
+
"Encryption/Readme.md",
|
|
84
|
+
"Forms/Readme.md",
|
|
85
|
+
"Framework/Readme.md",
|
|
86
|
+
"Mail/Readme.md",
|
|
87
|
+
"Models/Readme.md",
|
|
88
|
+
"Router/Readme.md",
|
|
89
|
+
"Storage/Readme.md",
|
|
90
|
+
"Support/Readme.md",
|
|
91
|
+
"Testing/Readme.md",
|
|
92
|
+
"Validation/Readme.md",
|
|
93
|
+
"Frontend/Angular/Readme.md",
|
|
94
|
+
"Frontend/Websockets/Readme.md"
|
|
77
95
|
]
|
|
78
96
|
}
|