Created a module for custom Ecto changeset validator functions
This commit is contained in:
		
							
								
								
									
										15
									
								
								lib/sukaato/custom_validators.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								lib/sukaato/custom_validators.ex
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
defmodule Sukaato.CustomValidators do
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  import Validate, only: [validate: 2]
 | 
			
		||||
 | 
			
		||||
  def validate_map_format(changeset, field, conventional_mapping) when is_atom(field) do
 | 
			
		||||
    field_value = get_field(changeset, field)
 | 
			
		||||
    validation_result = validate(field_value, conventional_mapping)
 | 
			
		||||
 | 
			
		||||
    if elem(validation_result, 0) == :ok do
 | 
			
		||||
      changeset
 | 
			
		||||
    else
 | 
			
		||||
      add_error(changeset, field, "is not a valid map")
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user