joaquimadraz / compel Goto Github PK
View Code? Open in Web Editor NEWRuby Object Coercion and Validation
License: MIT License
Ruby Object Coercion and Validation
License: MIT License
Hi @joaquimadraz ,
I was looking for hash validation library and even started building one from scratch. Coming from JS background I was looking for something that validate Hash
, most of libraries I found only works with Class
. This library looks great and thanks for that!
One question, as part of my validation I want to be able to validate unique fields in database.
For example I want to validate that param "email" is a valid email address and also is unique in "users" table/collection.
The reason I want it in the same library is because I want to return the validation errors to the client all in the same format of:
{
"errors": {
"name": [
"name is required"
],
"email": [
"email must be valid email address",
"email must be unique"
]
}
}
Is it possible to add custom validation rules?
Thanks,
Ran.
@joaquimadraz first of all thanks for building this gem ;)
From the readme it seems that the only way to build a "schema" is through the deep method ".keys"
schema = Compel.hash.keys({
first_name: Compel.string.required,
last_name: Compel.string.required,
birth_date: Compel.datetime,
address: Compel.hash.keys({
line_one: Compel.string.required,
line_two: Compel.string.default('-'),
post_code: Compel.string.format(/^\d{4}-\d{3}$/).required,
country_code: Compel.string.in(['PT', 'GB']).default('PT')
})
})
Because in the future I'll probably forget that to build a schema I'll have to access a ".hash" method that in turn has a ".keys" method, I propose a ".schema" shortcut method that does the same.
This is a bias proposal, in the future I will still remember that ".schema" builds a schema but this might not be true for other people.
Does the following proposal:
schema = Compel.schema({
first_name: Compel.string.required,
...
})
makes sense to anyone else?
Edit: I'm not suggesting the removal of ".hash.keys", just a alias method that does the same.
Something like:
Compel.hash.keys({
email: Compel.string,
username: Compel.string
}).one_of(:email, :username)
I made RestMyCase start using Compel and now my tests brake when using ruby 1.9.3.
If this is a too big of a change, don't worry about it but if not, please consider supporting ruby 1.9.3
Hi! first of all, thanks for this amazing gem, it's the best gem I could find to validate params in Sinatra in a clear and succinct way.
I was wondering if it's possible to validate the hash elements of an array depending on the type of the element. It's similar to issue #6. Example:
[{
type: 'A',
attribute1: 'random1'
},
{
type: 'A',
attribute1: 'random1'
},
{
type: 'A',
attribute1: 'random1'
},
{
type: 'B',
attribute2: 'random2'
},
{
type: 'B',
attribute2: 'random2'
}]
I would like to validate the elements individually depending on the type:
Do you think it's possible with the current implementation? the if
condition just lets me put a condition on the current value but I don't have access to the parent object to check the type
key. Alternatively, I can do a find_all
by type and validate them by type but it's more verbose.
Thanks
something like this:
Compel.hash.keys({
y: Compel.string.in('A', 'B', 'Z'),
x: Compel.string.in('D', 'E', 'F').required.if(:y, 'Z')
})
and with a lambda:
Compel.hash.keys({
y: Compel.string.in('A', 'B', 'Z'),
x: Compel.string.in('D', 'E', 'F').required.if(->(obj){ obj[:y] == 'Z' })
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.