Updated validate

This commit is contained in:
thomashii 2021-07-09 13:39:39 +08:00
parent 6272987ccd
commit 01040973b7
6 changed files with 56 additions and 62 deletions

View file

@ -17,7 +17,7 @@
* Migrated angel_framework to 4.0.0 (149/150 tests passed) * Migrated angel_framework to 4.0.0 (149/150 tests passed)
* Migrated angel_auth to 4.0.0 (31/31 tests passed) * Migrated angel_auth to 4.0.0 (31/31 tests passed)
* Migrated angel_configuration to 4.0.0 (8/8 testspassed) * Migrated angel_configuration to 4.0.0 (8/8 testspassed)
* Migrated angel_validate to 4.0.0 (6/7 tests passed) * Migrated angel_validate to 4.0.0 (7/7 tests passed)
* Migrated json_god to 4.0.0 (13/13 tests passed) * Migrated json_god to 4.0.0 (13/13 tests passed)
* Migrated angel_client to 4.0.0 (6/13 tests passed) * Migrated angel_client to 4.0.0 (6/13 tests passed)
* Migrated angel_websocket to 4.0.0 (2/3 tests passed) * Migrated angel_websocket to 4.0.0 (2/3 tests passed)

View file

@ -1,33 +1,50 @@
# 4.0.1 # Change Log
## 4.0.2
* Updated broken links in README
* All 7 unit test passed
## 4.0.1
* Updated README * Updated README
# 4.0.0 ## 4.0.0
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD
# 3.0.0 ## 3.0.0
* Migrated to work with Dart SDK 2.12.x Non NNBD * Migrated to work with Dart SDK 2.12.x Non NNBD
# 2.0.2 ## 2.0.2
* Deduplicate error messages. * Deduplicate error messages.
# 2.0.1+1 ## 2.0.1+1
* Fix bug in the implementation of `maxLength`. * Fix bug in the implementation of `maxLength`.
# 2.0.1 ## 2.0.1
* Patch for updated body parsing. * Patch for updated body parsing.
# 2.0.0 ## 2.0.0
* Finish update for Angel 2. * Finish update for Angel 2.
# 2.0.0-alpha.1 ## 2.0.0-alpha.1
* Update for Angel 2. * Update for Angel 2.
# 1.0.5-beta ## 1.0.5-beta
* Use `wrapMatcher` on explicit values instead of throwing. * Use `wrapMatcher` on explicit values instead of throwing.
* Add async matchers. * Add async matchers.
* Add context-aware matchers. * Add context-aware matchers.
# 1.0.4 ## 1.0.4
* `isNonEmptyString` trims strings. * `isNonEmptyString` trims strings.
* `ValidationException` extends `AngelHttpException`. * `ValidationException` extends `AngelHttpException`.
* Added `requireField` and `requireFields`. * Added `requireField` and `requireFields`.

View file

@ -1,23 +1,18 @@
# angel3_validate # Angel3 Request Validator
[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_validate) [![version](https://img.shields.io/badge/pub-v4.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_validate)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/validate/LICENSE) [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/validate/LICENSE)
[Live Example](https://angel-dart.github.io/validate) Validation library based on the `matcher` library, with Angel3 support. Why re-invent the wheel, when you can use the same validators you already use for tests?
Validation library based on the `matcher` library, with Angel support. This library runs both on the server, and on the client. Thus, you can use the same validation rules for forms on the server, and on the frontend.
Why re-invent the wheel, when you can use the same validators you already
use for tests?
This library runs both on the server, and on the client. Thus, you can use
the same validation rules for forms on the server, and on the frontend.
For convenience's sake, this library also exports `matcher`. For convenience's sake, this library also exports `matcher`.
- [angel3_validate](#angel3_validate) - [Angel3 Request Validator](#angel3-request-validator)
- [Examples](#examples) - [Examples](#examples)
- [Creating a Validator](#creating-a-validator) - [Creating a Validator](#creating-a-validator)
- [Validating data](#validating-data) - [Validating data](#validating-data)
@ -58,9 +53,7 @@ main() {
### Validating data ### Validating data
The `Validator` will filter out fields that have no validation rules. The `Validator` will filter out fields that have no validation rules. You can rest easy knowing that attackers cannot slip extra data into your applications.
You can rest easy knowing that attackers cannot slip extra data into
your applications.
```dart ```dart
main() { main() {
@ -92,8 +85,7 @@ main() {
Fields are optional by default. Fields are optional by default.
Suffix a field name with a `'*'` to mark it as required, and Suffix a field name with a `'*'` to mark it as required, and to throw an error if it is not present.
to throw an error if it is not present.
```dart ```dart
main() { main() {
@ -108,13 +100,11 @@ main() {
### Forbidden Fields ### Forbidden Fields
To prevent a field from showing up in valid data, suffix it To prevent a field from showing up in valid data, suffix it with a `'!'`.
with a `'!'`.
### Default values ### Default values
If not present, default values will be filled in *before* validation. If not present, default values will be filled in *before* validation. This means that they can still be used with required fields.
This means that they can still be used with required fields.
```dart ```dart
final Validator todo = Validator({ final Validator todo = Validator({
@ -125,17 +115,13 @@ final Validator todo = Validator({
}); });
``` ```
Default values can also be parameterless, *synchronous* functions Default values can also be parameterless, *synchronous* functions that return a single value.
that return a single value.
### Custom Validator Functions ### Custom Validator Functions
Creating a whole `Matcher` class is sometimes cumbersome, but if Creating a whole `Matcher` class is sometimes cumbersome, but if you pass a function to the constructor, it will be wrapped in a `Matcher` instance.
you pass a function to the constructor, it will be wrapped in a
`Matcher` instance.
(It simply returns the value of calling (It simply returns the value of calling [`predicate`](https://pub.dev/documentation/matcher/latest/matcher/predicate.html).)
[`predicate`](https://www.dartdocs.org/documentation/matcher/0.12.0%2B2/matcher/predicate.html).)
The function must *synchronously* return a `bool`. The function must *synchronously* return a `bool`.
@ -152,8 +138,7 @@ main() {
### Custom Error Messages ### Custom Error Messages
If these are not present, `angel3_validate` will *attempt* to generate If these are not present, `angel3_validate` will *attempt* to generate a coherent error message on its own.
a coherent error message on its own.
```dart ```dart
Validator({ Validator({
@ -196,9 +181,7 @@ print(only); // { foo: bar }
### Extending Validators ### Extending Validators
You can add situation-specific rules within a child validator. You can add situation-specific rules within a child validator. You can also use `extend` to mark fields as required or forbidden that originally were not. Default value and custom error message extension is also supported.
You can also use `extend` to mark fields as required or forbidden that originally
were not. Default value and custom error message extension is also supported.
```dart ```dart
final Validator userValidator = Validator({ final Validator userValidator = Validator({
@ -210,8 +193,7 @@ final Validator userValidator = Validator({
}); });
``` ```
To mark a field as now optional, and no longer required, To mark a field as now optional, and no longer required, suffix its name with a `'?'`.
suffix its name with a `'?'`.
```dart ```dart
var ageIsOptional = userValidator.extend({ var ageIsOptional = userValidator.extend({
@ -222,9 +204,7 @@ var ageIsOptional = userValidator.extend({
}); });
``` ```
Note that by default, validation rules are simply appended to Note that by default, validation rules are simply appended to the existing list. To completely overwrite existing rules, set the `overwrite` flag to `true`.
the existing list. To completely overwrite existing rules, set the
`overwrite` flag to `true`.
```dart ```dart
register(Map userData) { register(Map userData) {
@ -236,8 +216,7 @@ register(Map userData) {
### Bundled Matchers ### Bundled Matchers
This library includes some `Matcher`s for common validations, This library includes some `Matcher`s for common validations, including:
including:
- `isAlphaDash`: Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores. - `isAlphaDash`: Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores.
- `isAlphaNum`: Asserts that a `String` is alphanumeric. - `isAlphaNum`: Asserts that a `String` is alphanumeric.
@ -249,14 +228,11 @@ including:
- `isNonEmptyString`: Asserts that a value is a non-empty `String`. - `isNonEmptyString`: Asserts that a value is a non-empty `String`.
- `isUrl`: Asserts that a `String` is an HTTPS or HTTP URL. - `isUrl`: Asserts that a `String` is an HTTPS or HTTP URL.
The remaining functionality is The remaining functionality is [effectively implemented by the `matcher` package](https://pub.dev/documentation/matcher/latest/matcher/matcher-library.html).
[effectively implemented by the `matcher` package](https://www.dartdocs.org/documentation/matcher/0.12.0%2B2/matcher/matcher-library.html).
### Nested Validators ### Nested Validators
Very often, the data we validate contains other data within. You can pass Very often, the data we validate contains other data within. You can pass a `Validator` instance to the constructor, because it extends the `Matcher` class.
a `Validator` instance to the constructor, because it extends the
`Matcher` class.
```dart ```dart
main() { main() {

View file

@ -1,7 +1,8 @@
name: angel3_validate name: angel3_validate
description: Cross-platform request body validation library based on `matcher`. description: Cross-platform request body validation library based on `matcher`.
version: 4.0.1 version: 4.0.2
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/validate homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/validate
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
@ -10,9 +11,9 @@ dependencies:
matcher: ^0.12.0 matcher: ^0.12.0
dev_dependencies: dev_dependencies:
#angel3_test: #angel3_test:
angel3_mock_request: ^2.0.0
build_runner: ^1.11.1 build_runner: ^1.11.1
build_web_compilers: ^2.12.2 build_web_compilers: ^2.12.2
logging: ^1.0.1 logging: ^1.0.1
angel3_mock_request: ^2.0.0 pedantic: ^1.11.0
pedantic: ^1.0.0
test: ^1.17.4 test: ^1.17.4

View file

@ -12,7 +12,7 @@ import 'package:test/test.dart';
final Validator echoSchema = Validator({'message*': isString}); final Validator echoSchema = Validator({'message*': isString});
void printRecord(LogRecord rec) { void printRecord(LogRecord rec) {
print(rec); print('${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
if (rec.error != null) print(rec.error); if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace); if (rec.stackTrace != null) print(rec.stackTrace);
} }