@robinpath/schema
0.1.1Node.jsPublicLightweight schema validation: validate data against type schemas with constraints
Schema
Lightweight schema validation: validate data against type schemas with constraints
Package: @robinpath/schema | Category: Utility | Type: Utility
Authentication
No authentication required. All functions are available immediately.
Use Cases
Use the schema module when you need to:
- Validate data against a schema -- Use
schema.validateto perform this operation - Check if data matches schema (boolean) -- Use
schema.isValidto perform this operation - Create a string schema -- Use
schema.stringto perform this operation - Create a number schema -- Use
schema.numberto perform this operation - Create a boolean schema -- Use
schema.booleanto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
validate | Validate data against a schema | {valid: boolean, errors: string[]} |
isValid | Check if data matches schema (boolean) | True if valid |
string | Create a string schema | String schema object |
number | Create a number schema | Number schema object |
boolean | Create a boolean schema | Boolean schema object |
array | Create an array schema | Array schema object |
object | Create an object schema | Object schema object |
nullable | Make a schema also accept null | Nullable schema |
oneOf | Create a schema matching one of several schemas | OneOf schema |
getErrors | Validate and return only the errors array | Array of error strings |
Functions
validate
Validate data against a schema
Module: schema | Returns: object -- {valid: boolean, errors: string[]}
schema.validate $data $schema
| Parameter | Type | Required | Description |
|---|---|---|---|
data | any | Yes | Data to validate |
schema | object | Yes | Schema object |
isValid
Check if data matches schema (boolean)
Module: schema | Returns: boolean -- True if valid
schema.isValid $data $schema
| Parameter | Type | Required | Description |
|---|---|---|---|
data | any | Yes | Data to validate |
schema | object | Yes | Schema object |
string
Create a string schema
Module: schema | Returns: object -- String schema object
schema.string {"minLength": 1}
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | No | Options: {minLength, maxLength, pattern, enum} |
number
Create a number schema
Module: schema | Returns: object -- Number schema object
schema.number {"min": 0, "max": 100}
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | No | Options: {min, max, integer} |
boolean
Create a boolean schema
Module: schema | Returns: object -- Boolean schema object
schema.boolean
| Parameter | Type | Required | Description |
|---|---|---|---|
| (none) | No | Call with no arguments |
array
Create an array schema
Module: schema | Returns: object -- Array schema object
schema.array {"items": {"type": "string"}}
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | No | Options: {items} |
object
Create an object schema
Module: schema | Returns: object -- Object schema object
schema.object $opts
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | Yes | Options: {properties, required} |
nullable
Make a schema also accept null
Module: schema | Returns: object -- Nullable schema
schema.nullable $stringSchema
| Parameter | Type | Required | Description |
|---|---|---|---|
schema | object | Yes | Schema to wrap |
oneOf
Create a schema matching one of several schemas
Module: schema | Returns: object -- OneOf schema
schema.oneOf $schema1 $schema2
| Parameter | Type | Required | Description |
|---|---|---|---|
schemas | object | Yes | Schemas (pass as multiple args) |
getErrors
Validate and return only the errors array
Module: schema | Returns: array -- Array of error strings
schema.getErrors $data $schema
| Parameter | Type | Required | Description |
|---|---|---|---|
data | any | Yes | Data to validate |
schema | object | Yes | Schema object |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
| (standard errors) | Check function parameters and authentication |
@desc "Validate and validate result"
do
set $result as schema.validate $data $schema
if $result != null
print "Success"
else
print "No result"
end
enddo
Recipes
1. List and iterate Errors
Retrieve all items and loop through them.
@desc "Get errors and iterate results"
do
set $result as schema.getErrors $data $schema
each $item in $result
print $item
end
enddo
2. Multi-step Schema workflow
Chain multiple schema operations together.
@desc "Validate, is valid, and more"
do
set $r_validate as schema.validate $data $schema
set $r_isValid as schema.isValid $data $schema
set $r_string as schema.string {"minLength": 1}
print "All operations complete"
enddo
3. Safe validate with validation
Check results before proceeding.
@desc "Validate and validate result"
do
set $result as schema.validate $data $schema
if $result != null
print "Success: " + $result
else
print "Operation returned no data"
end
enddo
Related Modules
- json -- JSON module for complementary functionality
Versions (1)
| Version | Tag | Published |
|---|---|---|
| 0.1.1 | latest | 1 months ago |
Related Modules
@robinpathv0.1.4
SMTP email sending and address parsing for RobinPath
hash
JS@robinpathv0.1.3
Cryptographic hashing utilities: MD5, SHA family, HMAC, CRC32, file hashing, UUID v5 generation, secure random bytes, and content fingerprinting
csv
JS@robinpathv0.1.2
Parse and stringify CSV data
apollo
JS@robinpathv0.1.2
Apollo module for RobinPath.
$ robinpath add @robinpath/schema
