How do I use my validator in the plugin?

Please help me guys
How do I use my validator in the plugin?

i added file in my plugin

lib/validators/plugin_ldap_setting_validator.rb 

use in settings.yml

  forum_ldap_validation:
    default: 'system'
    validator: 'PluginLdapSettingValidator' 

load file in plugin.rb

load File.expand_path('../lib/validators/plugin_ldap_setting_validator.rb', __FILE__) 

getting an error

! Unable to load application: NoMethodError: undefined method `lazy' for nil:NilClass
bundler: failed to load command: puma (/home/smart/.rbenv/versions/2.6.5/bin/puma)
NoMethodError: undefined method `lazy' for nil:NilClass
  /home/smart/discourse/lib/plugin_initialization_guard.rb:9:in `rescue in plugin_initialization_guard'
  /home/smart/discourse/lib/plugin_initialization_guard.rb:3:in `plugin_initialization_guard'
  /home/smart/discourse/config/application.rb:269:in `<class:Application>'
  /home/smart/discourse/config/application.rb:59:in `<module:Discourse>'
  /home/smart/discourse/config/application.rb:58:in `<top (required)>'
  /home/smart/discourse/config/environment.rb:4:in `require'
  /home/smart/discourse/config/environment.rb:4:in `<top (required)>'
  config.ru:6:in `require'
  config.ru:6:in `block in <main>'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:55:in `instance_eval'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:55:in `initialize'
  config.ru:in `new'
  config.ru:in `<main>'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:49:in `eval'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:49:in `new_from_string'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:40:in `parse_file'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/configuration.rb:321:in `load_rackup'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/configuration.rb:246:in `app'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/runner.rb:155:in `load_and_bind'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/single.rb:98:in `run'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/launcher.rb:172:in `run'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-
1 Like

key as ‘validator’ there is

and even such an example works

plugins:
  my_custom_username_validation:
    default: 'system'
    validator: 'UsernameSettingValidator'
1 Like

placed the file in the discourse/lib/validators/ldap_setting_validator.rband it worked,

 forum_ldap_validation:
    default: 'system'
    validator: 'LdapSettingValidator'   

how to make the code work from the plugin?

Hooray, it worked

code in plugin.rb

def forum_require(path)
  require Rails.root.join('plugins', 'forum', 'app', path).to_s
end

after_initialize do

  forum_require 'lib/ldap_setting_validator'
//code
end

ldap_setting_validator.rb -> path -> myplugin/app/lib/ldap_setting_validator.rb

thanks for the example -> https://github.com/gdpelican/babble/blob/master/plugin.rb#L17-L52

1 Like

I was searching for an example of how to do this yesterday. The example I found in the Discourse repo is here: GitHub - discourse/discourse-chat-integration. You’ll see that in that plugin’s plugin.rb file, the validator class is required with the following command:

require_relative "lib/discourse_chat/provider/slack/slack_enabled_setting_validator"

For Discourse to find the validator, it needs to be included before the call to after_initialize.

2 Likes