Skip to content

Commit

Permalink
add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lost22git committed Jan 24, 2024
1 parent a79ed40 commit 452655f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

A simple, not flexible, mediocre performance Object Fields Validator

## Features

* based on pragma and macro
* tags to group/filter validation rules
* compile-time: tagsFilter expr
* runtime: filterTags params
* custom validation function
* custom validation error message
* nested validation

## TODO

* i18n validation error message


14 changes: 9 additions & 5 deletions tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import unittest

import validate
import std/[sequtils, uri]
import std/[sequtils, strutils]
test "test-book":
type
Category = ref object
Expand All @@ -19,20 +19,24 @@ test "test-book":
onsale
sold

proc isUrl(v: string): bool =
parseUri(v).isAbsolute()
proc isHttpUrl(v: string): bool =
v.startswith("http://")

#!fmt: off

type
Book = object
isbn {.valid: @[regex(pattern = r"ISBN \d{3}-\d{10}", tags = ["show"])].}: string
url {.validFn(fn = "isUrl", tags = ["show"]).}: string
url {.validFn(fn = "isHttpUrl", tags = ["show"], msg = "the url is not http url").}: string
category {.valid: @[nonNil()].}: Category
tags {.valid: @[length(min = 2, max = 4, tags = ["show"])].}: seq[string]
price {.valid: @[frange(min = 5, max = 50, tags = ["hide"])].}: float
case status: Status
of onsale, sold:
count {.valid: @[range(min = 100, tags = ["hide"])].}: int

#!fmt: on

proc validate(
book: Book, filterTags: varargs[string]
): ValidateResult {.validate: "".}
Expand All @@ -46,7 +50,7 @@ test "test-book":
book =
Book(
isbn: "ISBN 979-8836539412",
url: "abc",
url: "ftp://127.0.0.1/books/1.pdf",
category: category,
tags: @["nim"],
price: 52'd,
Expand Down

0 comments on commit 452655f

Please sign in to comment.