anchor.diff:          {ok, X} -> {ok, num_replicas, X}
       end.
   
  +parse_validate_replica_index(Params, ReplicasNum, true = _IsNew) ->
  +    parse_validate_replica_index(
  +      proplists:get_value("replicaIndex", Params,
  +                          replicas_num_default(ReplicasNum)));
  +parse_validate_replica_index(_Params, _ReplicasNum, false = _IsNew) ->
  +    ignore.
  +
  +replicas_num_default({ok, num_replicas, 0}) ->
  +    "0";
  +replicas_num_default(_) ->
  +    "1".
  +
   parse_validate_replica_index("0") -> {ok, replica_index, false};
   parse_validate_replica_index("1") -> {ok, replica_index, true};
   parse_validate_replica_index(_ReplicaValue) -> {error, replicaIndex, <<"replicaIndex can only be 1 or 0">>}.
  +
  +parse_validate_threads_number(Params, IsNew) ->
  +    validate_with_missing(proplists:get_value("threadsNumber", Params),
  +                          "3", IsNew, fun parse_validate_threads_number/1).
   
   parse_validate_flush_enabled("0") -> {ok, flush_enabled, false};
   parse_validate_flush_enabled("1") -> {ok, flush_enabled, true};
  
positive.code:         _ -> basic_bucket_params_screening_tail(Ctx, Params, AuthType)
      end.
  
  basic_bucket_params_screening_tail(Ctx, Params, AuthType) ->
      BucketName = Ctx#bv_ctx.bucket_name,
      BucketConfig = Ctx#bv_ctx.bucket_config,
      IsNew = Ctx#bv_ctx.new,
      AllBuckets = Ctx#bv_ctx.all_buckets,
  
      Candidates0 = [{ok, name, BucketName},
                     {ok, auth_type, AuthType},
                     validate_with_missing(proplists:get_value("flushEnabled", Params), "0", IsNew, fun parse_validate_flush_enabled/1),
                     case IsNew of
                         true ->
                             case BucketConfig of
                                 false ->
                                     %% we'll give error on missing bucket name later
                                     case BucketName =:= undefined orelse ns_bucket:is_valid_bucket_name(BucketName) of
                                         {error, invalid} ->
                                             {error, name,
                                              <<"Bucket name can only contain characters in range A-Z, a-z, 0-9 as well as underscore, period, dash & percent. Consult the documentation.">>};
                                         {error, reserved} ->
                                             {error, name, <<"This name is reserved for the internal use.">>};
                                         {error, starts_with_dot} ->
                                             {error, name, <<"Bucket name cannot start with dot.">>};
                                         {error, empty} ->
                                             %% we'll give error on empty bucket name later
                                             ignore;
                                         _ ->
                                             %% we have to check for conflict here because we were looking
                                             %% for BucketConfig using case sensetive search (in basic_bucket_params_screening/4)
                                             %% but we do not allow buckets with the same names in a different register
                                             case BucketName =/= undefined andalso ns_bucket:name_conflict(BucketName, AllBuckets) of
                                                 false ->
                                                     ignore;
                                                 _ ->
                                                     {error, name, <<"Bucket with given name already exists">>}
                                             end
                                     end;
                                 _ ->
                                     {error, name, <<"Bucket with given name already exists">>}
                             end;
                         _ ->
                             true = (BucketConfig =/= false),
                             {ok, currentBucket, BucketConfig}
                     end,
                     case BucketName of
                         [] ->
                             {error, name, <<"Bucket name cannot be empty">>};
                         undefined ->
                             {error, name, <<"Bucket name needs to be specified">>};
                         _ ->
                             case string:len(BucketName) > ?MAX_BUCKET_NAME_LEN of
                                 true ->
                                     {error, name, ?l2b(io_lib:format("Bucket name cannot exceed ~p characters",
                                                                      [?MAX_BUCKET_NAME_LEN]))};
                                 _ -> ignore
                             end
                     end,
                     case AuthType of
                         none ->
                             ProxyPort = proplists:get_value("proxyPort", Params),
                             case ProxyPort of
                                 undefined when BucketConfig =/= false ->
                                     case ns_bucket:auth_type(BucketConfig) of
                                         AuthType -> ignore;
                                         _ ->
                                             {error, proxyPort,
                                              <<"port is missing">>}
                                     end;
                                 _ ->
                                     case (catch menelaus_util:parse_validate_port_number(ProxyPort)) of
                                         {error, [Error]} ->
                                             {error, proxyPort, Error};
                                         PP ->
                                             case ns_bucket:is_port_free(BucketName, PP) of
                                                 true ->
                                                     {ok, moxi_port, PP};
                                                 false ->
                                                     {error, proxyPort,
                                                      <<"port is already in use">>}
                                             end
                                     end
                             end;
                         sasl ->
                             validate_with_missing(proplists:get_value("saslPassword", Params), "",
                                                   IsNew, fun validate_bucket_password/1)
                     end,
                     parse_validate_ram_quota(proplists:get_value("ramQuotaMB", Params),
                                              BucketConfig),
                     parse_validate_other_buckets_ram_quota(
                       proplists:get_value("otherBucketsRamQuotaMB", Params))],
      BucketType = if
                       (not IsNew) andalso BucketConfig =/= false ->
                           ns_bucket:bucket_type(BucketConfig);
                       true ->
                           case proplists:get_value("bucketType", Params) of
                               "memcached" -> memcached;
                               "membase" -> membase;
                               "couchbase" -> membase;
                               undefined -> membase;
                               _ -> invalid
                           end
                   end,
      QuotaSizeError = case lists:keyfind(ram_quota, 2, Candidates0) of
                           {ok, ram_quota, RAMQuotaMB} ->
                               {MinQuota, Msg}
                                   = case BucketType of
                                         membase ->
                                             Q = misc:get_env_default(membase_min_ram_quota, 100),
                                             Qv = list_to_binary(integer_to_list(Q)),
                                             {Q, <<"RAM quota cannot be less than ", Qv/binary, " MB">>};
                                         memcached ->
                                             Q = misc:get_env_default(memcached_min_ram_quota, 64),
                                             Qv = list_to_binary(integer_to_list(Q)),
                                             {Q, <<"RAM quota cannot be less than ", Qv/binary, " MB">>};
                                         _ -> {0, <<"">>}
                                     end,
                               if
                                   RAMQuotaMB < MinQuota * ?MIB ->
                                       {error, ramQuotaMB, Msg};
                                   IsNew =/= true andalso BucketConfig =/= false andalso BucketType =:= memcached ->
                                       case ns_bucket:raw_ram_quota(BucketConfig) of
                                           RAMQuotaMB -> ignore;
                                           _ ->
                                               {error, ramQuotaMB, <<"cannot change quota of memcached buckets">>}
                                       end;
                                   true ->
                                       ignore
                               end;
                           _ -> ignore
                       end,
      Candidates1 = [QuotaSizeError | Candidates0],
      Candidates2 = case menelaus_web:parse_validate_bucket_auto_compaction_settings(Params) of
                        nothing -> Candidates1;
                        false -> [{ok, autocompaction, false},
                                  {ok, purge_interval, undefined} | Candidates1];
                        {errors, Errors} ->
                            [{error, F, M} || {F, M} <- Errors] ++ Candidates1;
                        {ok, ACSettings, MaybePurgeInterval} ->
                            case MaybePurgeInterval of
                                [{purge_interval, PurgeInterval}] ->
                                    [{ok, purge_interval, PurgeInterval}];
                                [] ->
                                    []
                            end ++ [{ok, autocompaction, ACSettings} | Candidates1]
                    end,
      Candidates = case BucketType of
                       memcached ->
                           [{ok, bucketType, memcached}
                            | Candidates1];
                       membase ->
                           ReplicasNumResult =
                               validate_with_missing(
                                 proplists:get_value("replicaNumber", Params),
                                 %% replicaNumber doesn't have
                                 %% default. Has to be given for
                                 %% creates, but may be omitted for
                                 %% updates. Later is for backwards
                                 %% compat, the former is from earlier
                                 %% code and stricter requirements is
                                 %% IMO ok to keep.
                                 undefined,
                                 IsNew,
                                 fun parse_validate_replicas_number/1),
                           [{ok, bucketType, membase},
                            ReplicasNumResult,
                            get_time_sync(Params, IsNew),
                            case IsNew of
                                true ->
                                    ReplicaIndexDefault =
                                        case ReplicasNumResult of
                                            {ok, num_replicas, 0} ->
                                                "0";
                                            _ ->
                                                "1"
                                        end,
                                    parse_validate_replica_index(proplists:get_value("replicaIndex", Params, ReplicaIndexDefault));
                                false ->
                                    ignore
                            end,
                            validate_with_missing(proplists:get_value("threadsNumber", Params), "3", IsNew, fun parse_validate_threads_number/1),
                            validate_with_missing(proplists:get_value("evictionPolicy", Params), "valueOnly", IsNew, fun parse_validate_eviction_policy/1)
                            | Candidates2];
                       _ ->
                           [{error, bucketType, <<"invalid bucket type">>}
                            | Candidates2]
                   end,
      %% this is to validate that Candidates elements have specific
      %% structure
      [case E of
           %% ok-s are used to keep correctly parsed/validated params
           {ok, _, _} -> [];
           %% error-s hold errors
           {error, _, _} -> [];
           %% ignore-s are used to "do nothing"
           ignore -> []
       end || E <- Candidates],
      {[{K,V} || {ok, K, V} <- Candidates],
       [{K,V} || {error, K, V} <- Candidates]}.
  
  get_time_sync(Params, true = _IsNew) ->
      case proplists:get_value("timeSynchronization", Params) of
          undefined ->
              {ok, time_synchronization, disabled};
  
negative.code:             {error, time_synchronization,
               <<"TimeSyncronization not allowed in update bucket">>}
      end.
  
  assert_candidates(Candidates) ->
      %% this is to validate that Candidates elements have specific
      %% structure
      [case E of
           %% ok-s are used to keep correctly parsed/validated params
           {ok, _, _} -> [];
           %% error-s hold errors
           {error, _, _} -> [];
           %% ignore-s are used to "do nothing"
           ignore -> []
       end || E <- Candidates].
  
  validate_bucket_password(undefined) ->
      {error, saslPassword, <<"Bucket password is undefined">>};
  