Comments (9)
Hi, @23tux!
Right now StoreModel
focuses on isolating JSON(B) data from the parent model. In your example an array (where it is stored?) is used inside the ApplicationRecord
subclass. Probably custom Rails validator will help here:
class User < ApplicationRecord
...
validates :roles, array: { inclusion: { in: ROLES }
end
from store_model.
I guess that enumerize
gem could deal with it. I didn't try though :)
from store_model.
@DmitryTsepelev #roles
is a JSON column on the users
table. I thought because you have support for enum types on single attributes (in the example here https://github.com/DmitryTsepelev/store_model/blob/master/docs/enums.md it is the key status
), it would be a good fit to extend this to support an array of enums.
Consider this example, maybe it's clearer this way:
class Configuration
include StoreModel::Model
enum :role, %i[admin user reporter], default: :user
end
class User < ApplicationRecord
attribute :configuration, Configuration.to_type
end
My user has a configuration with an enum of role
that defaults to :user
. What if I want multiple roles for a user? I thought an array of enums would be a good solution for this. What do you think?
from store_model.
Aha, in this case having the built-in inclusion validator for arrays makes sense to me!
from store_model.
@FunkyloverOne you are right, enumerize has built in support for Arrays when you serialize them or use mongodb. I still have to find out if it works with json array columns though. But because we already have the store_model gem in our Gemfile, adding another dependency for this simple use case isn't something I'm very happy about
from store_model.
@DmitryTsepelev How would you implement this? If you can give me a push in the right direction, I'm happy to try for a PR.
from store_model.
@23tux This is what should be done to make it work:
- Take a look at docs about custom validators
- Add a new custom validator (we already have one) called
ArrayValidator
- Implement the validator (there is a good example)
- Add specs
- Bonus point: add docs
🙂
As a result, it will be possible to add a validation to the StoreModel
in a following way:
class Configuration
include StoreModel::Model
ROLES = %i[admin user reporter]
enum :role, ROLES, default: :user
validates :roles, array: { inclusion: { in: ROLES }
end
class User < ApplicationRecord
attribute :configuration, Configuration.to_type
end
Bonus point: it might be helpful to have a shortcut for the enum
method to use this validation by default enum :role, ROLES, default: :user, validate: true
from store_model.
@DmitryTsepelev thanks for the kick off!
I'm not sure if I understand the need of a validator when using enums. The current implementation raises an error when an invalid value is assigned, e.g. from your examples
class Configuration
include StoreModel::Model
enum :status, %i[active archived], default: :active
end
config = Configuration.new
config.status = "foo"
# ArgumentError: invalid value 'foo' is assigned
# from /usr/local/bundle/gems/store_model-0.7.0/lib/store_model/types/enum_type.rb:56:in `raise_invalid_value!'
I would suggest to keep the same behaviour for an array of enums: When you push an invalid value into the array, it should raise an error. Do you know what I mean?
from store_model.
@23tux Sorry for the long response, that's a valid point! We don't need to use a validator in the current implementation (however, there is a chance that someone would need to make this behaviour optional in the future)
from store_model.
Related Issues (20)
- Presence validation fails if value is false HOT 2
- How to attach active storage files? HOT 3
- Array of array support HOT 3
- One of depending on another model's column HOT 4
- Validate any Hash HOT 1
- Encrypt attributes HOT 2
- Aliasing an attribute HOT 1
- GraphQL input type fails to cast HOT 4
- ArgumentError: wrong number of arguments (given 1, expected 0) in random model HOT 3
- Assignment Doesn't work in OneOf case HOT 3
- Delegation of `fetch` to `attributes` causes issues HOT 2
- NameError: uninitialized constant StoreModel::Types::ArrayType HOT 3
- Default values for attributes when retrieving the store model HOT 1
- Decoding Custom Types HOT 1
- Allow an option to disallow "UnknownAttributes" behavior HOT 1
- ActiveRecord validation contexts don't propagate HOT 1
- Override methods HOT 3
- Broken defaults in 2.0 HOT 2
- Attribute encryption with ActiveRecord::Encryption HOT 1
- StoreModel Stringifying json when saving. HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store_model.