Modules@robinpath/schema
schema

@robinpath/schema

0.1.1Node.jsPublic

Lightweight 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.validate to perform this operation
  • Check if data matches schema (boolean) -- Use schema.isValid to perform this operation
  • Create a string schema -- Use schema.string to perform this operation
  • Create a number schema -- Use schema.number to perform this operation
  • Create a boolean schema -- Use schema.boolean to perform this operation

Quick Reference

FunctionDescriptionReturns
validateValidate data against a schema{valid: boolean, errors: string[]}
isValidCheck if data matches schema (boolean)True if valid
stringCreate a string schemaString schema object
numberCreate a number schemaNumber schema object
booleanCreate a boolean schemaBoolean schema object
arrayCreate an array schemaArray schema object
objectCreate an object schemaObject schema object
nullableMake a schema also accept nullNullable schema
oneOfCreate a schema matching one of several schemasOneOf schema
getErrorsValidate and return only the errors arrayArray of error strings

Functions

validate

Validate data against a schema

Module: schema | Returns: object -- {valid: boolean, errors: string[]}

schema.validate $data $schema
ParameterTypeRequiredDescription
dataanyYesData to validate
schemaobjectYesSchema object

isValid

Check if data matches schema (boolean)

Module: schema | Returns: boolean -- True if valid

schema.isValid $data $schema
ParameterTypeRequiredDescription
dataanyYesData to validate
schemaobjectYesSchema object

string

Create a string schema

Module: schema | Returns: object -- String schema object

schema.string {"minLength": 1}
ParameterTypeRequiredDescription
optionsobjectNoOptions: {minLength, maxLength, pattern, enum}

number

Create a number schema

Module: schema | Returns: object -- Number schema object

schema.number {"min": 0, "max": 100}
ParameterTypeRequiredDescription
optionsobjectNoOptions: {min, max, integer}

boolean

Create a boolean schema

Module: schema | Returns: object -- Boolean schema object

schema.boolean
ParameterTypeRequiredDescription
(none)NoCall with no arguments

array

Create an array schema

Module: schema | Returns: object -- Array schema object

schema.array {"items": {"type": "string"}}
ParameterTypeRequiredDescription
optionsobjectNoOptions: {items}

object

Create an object schema

Module: schema | Returns: object -- Object schema object

schema.object $opts
ParameterTypeRequiredDescription
optionsobjectYesOptions: {properties, required}

nullable

Make a schema also accept null

Module: schema | Returns: object -- Nullable schema

schema.nullable $stringSchema
ParameterTypeRequiredDescription
schemaobjectYesSchema to wrap

oneOf

Create a schema matching one of several schemas

Module: schema | Returns: object -- OneOf schema

schema.oneOf $schema1 $schema2
ParameterTypeRequiredDescription
schemasobjectYesSchemas (pass as multiple args)

getErrors

Validate and return only the errors array

Module: schema | Returns: array -- Array of error strings

schema.getErrors $data $schema
ParameterTypeRequiredDescription
dataanyYesData to validate
schemaobjectYesSchema object

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(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)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/schema

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size4.7 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities