Skip to content

Commit ba5c248

Browse files
committed
done readme
1 parent f442263 commit ba5c248

File tree

2 files changed

+112
-19
lines changed

2 files changed

+112
-19
lines changed

.github/copilot-instructions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ component {
2020
email: { required: true, type: "email" },
2121
age: { required: true, min: 18, max: 65 }
2222
};
23-
23+
2424
// Profile-based field selection for targeted validation
2525
this.constraintProfiles = {
2626
registration: "email,password,confirmPassword",
27-
update: "email,firstName,lastName"
27+
update: "email,firstName,lastName"
2828
};
2929
}
3030

@@ -49,7 +49,7 @@ moduleSettings = {
4949

5050
### Validation Workflow
5151
1. **Constraint Resolution**: `determineConstraintsDefinition()` - object properties → shared constraints → inline structs
52-
2. **Profile Processing**: Constraint profiles expand to `includeFields` for targeted validation scenarios
52+
2. **Profile Processing**: Constraint profiles expand to `includeFields` for targeted validation scenarios
5353
3. **Rule Processing**: `processRules()` iterates constraints, delegating to specific validators
5454
4. **Result Aggregation**: ValidationResult accumulates errors with i18n message formatting
5555

@@ -81,7 +81,7 @@ email: {
8181
}
8282

8383
// Custom validation methods
84-
password: {
84+
password: {
8585
method: "validatePasswordStrength", // Call this.validatePasswordStrength()
8686
udf: variables.customValidator // Direct function reference
8787
}
@@ -105,7 +105,7 @@ password: {
105105
box install
106106
box server start [email protected]
107107

108-
# Run tests during development
108+
# Run tests during development
109109
box testbox run
110110

111111
# Format code to standards
@@ -159,7 +159,7 @@ function apiCreateUser(event, rc, prc) {
159159
return event.renderData(data=user);
160160
} catch(ValidationException e) {
161161
return event.renderData(
162-
statusCode=422,
162+
statusCode=422,
163163
data={errors: deserializeJSON(e.extendedInfo)}
164164
);
165165
}
@@ -180,5 +180,5 @@ function apiCreateUser(event, rc, prc) {
180180

181181
### Testing Validators
182182
- Extend `coldbox.system.testing.BaseTestCase` for integration tests
183-
- Mock ValidationResult for unit testing individual validators
183+
- Mock ValidationResult for unit testing individual validators
184184
- Test constraint edge cases: null values, empty strings, boundary conditions

readme.md

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
[![Total Downloads](https://forgebox.io/api/v1/entry/cbvalidation/badges/downloads)](https://forgebox.io/view/cbvalidation)
2+
[![Latest Stable Version](https://forgebox.io/api/v1/entry/cbvalidation/badges/version)](https://forgebox.io/view/cbvalidation)
3+
[![Apache2 License](https://img.shields.io/badge/License-Apache2-blue.svg)](https://forgebox.io/view/cbvalidation)
4+
15
<p align="center">
2-
<img src="https://www.ortussolutions.com/__media/coldbox-185-logo.png">
3-
<br>
4-
<img src="https://www.ortussolutions.com/__media/wirebox-185.png" height="125">
5-
<img src="https://www.ortussolutions.com/__media/cachebox-185.png" height="125" >
6-
<img src="https://www.ortussolutions.com/__media/logbox-185.png" height="125">
6+
<img src="https://www.ortussolutions.com/__media/coldbox-185-logo.png" alt="ColdBox Platform Logo">
77
</p>
88

99
<p align="center">
@@ -17,6 +17,8 @@
1717

1818
This module is a server side rules validation engine that can provide you with a unified approach to object, struct and form validation. You can construct validation constraint rules and then tell the engine to validate them accordingly. You can validate objects or structures and even use profiles to target specific fields for validation. The validation engine is highly extensible and can be used in any kind of application.
1919

20+
The module leverages the power of the cbi18n module for internationalization support, providing localized validation messages, and integrates seamlessly with ColdBox's WireBox dependency injection container for maximum flexibility and testability.
21+
2022
## License
2123

2224
Apache License, Version 2.0.
@@ -29,18 +31,27 @@ Apache License, Version 2.0.
2931

3032
## Requirements
3133

32-
- BoxLang 1+
33-
- Lucee 5.x+
34-
- Adobe ColdFusion 2021+
34+
- **BoxLang** 1.0+ (Preferred)
35+
- **Lucee** 5.x+
36+
- **Adobe ColdFusion** 2021+
37+
- **Dependencies**: ColdBox 7+, cbi18n 3.0+
3538

3639
## Installation
3740

3841
Leverage CommandBox to install:
3942

40-
`box install cbvalidation`
43+
```bash
44+
box install cbvalidation
45+
```
4146

4247
The module will register several objects into WireBox using the `@cbvalidation` namespace. The validation manager is registered as `ValidationManager@cbvalidation`. It will also register several helper methods that can be used throughout the ColdBox application: `validate(), validateOrFail(), getValidationManager()`
4348

49+
### WireBox Registrations
50+
51+
- `ValidationManager@cbvalidation` - The core validation engine
52+
- `validationManager@cbvalidation` - Alias for convenience
53+
- Global validation helper methods injected into all ColdBox components
54+
4455
## Mixins
4556

4657
The module will also register several methods in your handlers/interceptors/layouts/views
@@ -115,14 +126,22 @@ Here are the module settings you can place in your `ColdBox.cfc` by using the `c
115126

116127
```js
117128
modulesettings = {
118-
cbValidation = {
129+
cbvalidation = {
119130
// The third-party validation manager to use, by default it uses CBValidation.
120131
manager = "class path",
121132

122133
// You can store global constraint rules here with unique names
123134
sharedConstraints = {
124-
name = {
125-
field = { constraints here }
135+
userRegistration = {
136+
email = { required=true, type="email" },
137+
password = { required=true, size="8..50" },
138+
firstName = { required=true, size="1..50" },
139+
lastName = { required=true, size="1..50" }
140+
},
141+
userUpdate = {
142+
email = { required=true, type="email" },
143+
firstName = { required=true, size="1..50" },
144+
lastName = { required=true, size="1..50" }
126145
}
127146
}
128147
}
@@ -229,6 +248,80 @@ this.constraints = {
229248
}
230249
```
231250

251+
## Usage Examples
252+
253+
### Basic Validation in Handlers
254+
255+
```js
256+
function saveUser( event, rc, prc ){
257+
var results = validate( target=rc, constraints="userRegistration" );
258+
if( results.hasErrors() ){
259+
prc.errors = results.getAllErrors();
260+
return event.setView( "users/registration" );
261+
}
262+
// Process valid data
263+
var user = userService.create( rc );
264+
}
265+
```
266+
267+
### Exception Based Validation
268+
269+
```js
270+
function apiCreateUser( event, rc, prc ){
271+
try{
272+
var validData = validateOrFail( target=rc, profiles="registration" );
273+
var user = userService.create( validData );
274+
return event.renderData( data=user );
275+
} catch( ValidationException e ){
276+
return event.renderData(
277+
statusCode=422,
278+
data={ errors: deserializeJSON( e.extendedInfo ) }
279+
);
280+
}
281+
}
282+
```
283+
284+
### Object-Based Constraints
285+
286+
```js
287+
class {
288+
289+
this.constraints = {
290+
email = { required=true, type="email" },
291+
age = { required=true, min=18, max=65 },
292+
password = { required=true, size="8..50" },
293+
confirmPassword = { required=true, sameAs="password" },
294+
"address.street" = { required=true, size="5..100" },
295+
"preferences.*" = { type="string" }
296+
};
297+
298+
this.constraintProfiles = {
299+
registration = "email,password,confirmPassword",
300+
update = "email,firstName,lastName",
301+
passwordChange = "password,confirmPassword"
302+
};
303+
304+
}
305+
```
306+
307+
## BoxLang CLI Runner
308+
309+
Run your validation tests with the BoxLang CLI:
310+
311+
```bash
312+
# Run all tests
313+
boxlang-cli test run
314+
315+
# Run with specific runner
316+
boxlang-cli test run --runner="http://localhost:60299/tests/runner.cfm"
317+
318+
# Run specific test bundle
319+
boxlang-cli test run --bundles="test-harness/tests"
320+
321+
# Run with verbose output
322+
boxlang-cli test run --verbose
323+
```
324+
232325
## Constraint Profiles
233326

234327
You can also create profiles or selections of fields that will be targeted for validation if you are defining the constraints in objects. All you do is create a key called: `this.constraintProfiles` which contains a struct of defined fields:

0 commit comments

Comments
 (0)