@@ -1364,74 +1364,25 @@ def cluster_modifying?(new_options)
1364
1364
# but does not check for interactions between combinations of options.
1365
1365
def validate_new_options! ( opts )
1366
1366
return Options ::Redacted . new unless opts
1367
- if opts [ :read_concern ]
1368
- # Raise an error for non user-settable options
1369
- if opts [ :read_concern ] [ :after_cluster_time ]
1370
- raise Mongo ::Error ::InvalidReadConcern . new (
1371
- 'The after_cluster_time read_concern option cannot be specified by the user'
1372
- )
1373
- end
1374
-
1375
- given_keys = opts [ :read_concern ] . keys . map ( &:to_s )
1376
- allowed_keys = [ 'level' ]
1377
- invalid_keys = given_keys - allowed_keys
1378
- # Warn that options are invalid but keep it and forward to the server
1379
- unless invalid_keys . empty?
1380
- log_warn ( "Read concern has invalid keys: #{ invalid_keys . join ( ',' ) } ." )
1381
- end
1382
- end
1383
-
1384
- if server_api = opts [ :server_api ]
1385
- unless server_api . is_a? ( Hash )
1386
- raise ArgumentError , ":server_api value must be a hash: #{ server_api } "
1387
- end
1388
-
1389
- extra_keys = server_api . keys - %w( version strict deprecation_errors )
1390
- unless extra_keys . empty?
1391
- raise ArgumentError , "Unknown keys under :server_api: #{ extra_keys . map ( &:inspect ) . join ( ', ' ) } "
1392
- end
1393
1367
1394
- if version = server_api [ :version ]
1395
- unless VALID_SERVER_API_VERSIONS . include? ( version )
1396
- raise ArgumentError , "Unknown server API version: #{ version } "
1397
- end
1398
- end
1368
+ validate_read_concern! ( opts [ :read_concern ] )
1369
+ validate_server_api! ( opts [ :server_api ] )
1370
+ validate_max_min_pool_size! ( opts )
1371
+ validate_max_connecting! ( opts )
1372
+ validate_read! ( opts )
1373
+ validate_compressors! ( opts )
1374
+ validate_srv_max_hosts! ( opts )
1375
+
1376
+ invalid_options = opts . keys . map ( &:to_sym ) - VALID_OPTIONS
1377
+ if invalid_options . any?
1378
+ log_warn ( "Unsupported client options: #{ invalid_options . join ( ', ' ) } . These will be ignored." )
1379
+ opts = opts . select { |key , | VALID_OPTIONS . include? ( key . to_sym ) }
1399
1380
end
1400
1381
1401
1382
Lint . validate_underscore_read_preference ( opts [ :read ] )
1402
1383
Lint . validate_read_concern_option ( opts [ :read_concern ] )
1403
- opts . each . inject ( Options ::Redacted . new ) do |_options , ( k , v ) |
1404
- key = k . to_sym
1405
- if VALID_OPTIONS . include? ( key )
1406
- validate_max_min_pool_size! ( key , opts )
1407
- validate_max_connecting! ( key , opts )
1408
- validate_read! ( key , opts )
1409
- if key == :compressors
1410
- compressors = valid_compressors ( v )
1411
-
1412
- if compressors . include? ( 'snappy' )
1413
- validate_snappy_compression!
1414
- end
1415
-
1416
- if compressors . include? ( 'zstd' )
1417
- validate_zstd_compression!
1418
- end
1419
1384
1420
- _options [ key ] = compressors unless compressors . empty?
1421
- elsif key == :srv_max_hosts
1422
- if v && ( !v . is_a? ( Integer ) || v < 0 )
1423
- log_warn ( "#{ v } is not a valid integer for srv_max_hosts" )
1424
- else
1425
- _options [ key ] = v
1426
- end
1427
- else
1428
- _options [ key ] = v
1429
- end
1430
- else
1431
- log_warn ( "Unsupported client option '#{ k } '. It will be ignored." )
1432
- end
1433
- _options
1434
- end
1385
+ Options ::Redacted . new ( opts )
1435
1386
end
1436
1387
1437
1388
# Validates all options after they are set on the client.
@@ -1569,6 +1520,48 @@ def validate_options!(addresses = nil, is_srv: nil)
1569
1520
end
1570
1521
end
1571
1522
1523
+ ALLOWED_READ_CONCERN_KEYS = %w[ level ] . freeze
1524
+
1525
+ def validate_read_concern! ( read_concern )
1526
+ return unless read_concern
1527
+
1528
+ # Raise an error for non user-settable options
1529
+ if read_concern [ :after_cluster_time ]
1530
+ raise Mongo ::Error ::InvalidReadConcern . new (
1531
+ 'The after_cluster_time read_concern option cannot be specified by the user'
1532
+ )
1533
+ end
1534
+
1535
+ given_keys = read_concern . keys . map ( &:to_s )
1536
+ invalid_keys = given_keys - ALLOWED_READ_CONCERN_KEYS
1537
+
1538
+ # Warn that options are invalid but keep it and forward to the server
1539
+ unless invalid_keys . empty?
1540
+ log_warn ( "Read concern has invalid keys: #{ invalid_keys . join ( ',' ) } ." )
1541
+ end
1542
+ end
1543
+
1544
+ ALLOWED_SERVER_API_KEYS = %w[ version strict deprecation_errors ] . freeze
1545
+
1546
+ def validate_server_api! ( server_api )
1547
+ return unless server_api
1548
+
1549
+ unless server_api . is_a? ( Hash )
1550
+ raise ArgumentError , ":server_api value must be a hash: #{ server_api } "
1551
+ end
1552
+
1553
+ extra_keys = server_api . keys - ALLOWED_SERVER_API_KEYS
1554
+ unless extra_keys . empty?
1555
+ raise ArgumentError , "Unknown keys under :server_api: #{ extra_keys . map ( &:inspect ) . join ( ', ' ) } "
1556
+ end
1557
+
1558
+ if version = server_api [ :version ]
1559
+ unless VALID_SERVER_API_VERSIONS . include? ( version )
1560
+ raise ArgumentError , "Unknown server API version: #{ version } "
1561
+ end
1562
+ end
1563
+ end
1564
+
1572
1565
# Validates all authentication-related options after they are set on the client
1573
1566
# This method is intended to catch combinations of options which are not allowed
1574
1567
def validate_authentication_options!
@@ -1639,6 +1632,22 @@ def valid_compressors(compressors)
1639
1632
end
1640
1633
end
1641
1634
1635
+ def validate_compressors! ( opts )
1636
+ return unless opts [ :compressors ]
1637
+
1638
+ compressors = valid_compressors ( opts [ :compressors ] )
1639
+
1640
+ if compressors . include? ( 'snappy' )
1641
+ validate_snappy_compression!
1642
+ end
1643
+
1644
+ if compressors . include? ( 'zstd' )
1645
+ validate_zstd_compression!
1646
+ end
1647
+
1648
+ opts [ :compressors ] = compressors unless compressors . empty?
1649
+ end
1650
+
1642
1651
def validate_snappy_compression!
1643
1652
return if defined? ( Snappy )
1644
1653
require 'snappy'
@@ -1657,14 +1666,25 @@ def validate_zstd_compression!
1657
1666
"\" bundle install\" to install the gem. (#{ e . class } : #{ e } )"
1658
1667
end
1659
1668
1660
- def validate_max_min_pool_size! ( option , opts )
1661
- if option == :min_pool_size && opts [ :min_pool_size ]
1662
- max = opts [ :max_pool_size ] || Server ::ConnectionPool ::DEFAULT_MAX_SIZE
1663
- if max != 0 && opts [ :min_pool_size ] > max
1664
- raise Error ::InvalidMinPoolSize . new ( opts [ :min_pool_size ] , max )
1665
- end
1669
+ def validate_max_min_pool_size! ( opts )
1670
+ return unless opts [ :min_pool_size ]
1671
+
1672
+ max = opts [ :max_pool_size ] || Server ::ConnectionPool ::DEFAULT_MAX_SIZE
1673
+ if max != 0 && opts [ :min_pool_size ] > max
1674
+ raise Error ::InvalidMinPoolSize . new ( opts [ :min_pool_size ] , max )
1675
+ end
1676
+ end
1677
+
1678
+ def validate_srv_max_hosts! ( opts )
1679
+ return unless opts . key? ( :srv_max_hosts )
1680
+
1681
+ srv_max_hosts = opts [ :srv_max_hosts ]
1682
+ return unless srv_max_hosts
1683
+
1684
+ if !srv_max_hosts . is_a? ( Integer ) || srv_max_hosts < 0
1685
+ log_warn ( "#{ srv_max_hosts } is not a valid integer for srv_max_hosts" )
1686
+ opts . delete ( :srv_max_hosts )
1666
1687
end
1667
- true
1668
1688
end
1669
1689
1670
1690
# Validates whether the max_connecting option is valid.
@@ -1673,35 +1693,34 @@ def validate_max_min_pool_size!(option, opts)
1673
1693
# @param [ Hash ] opts The client options.
1674
1694
#
1675
1695
# @return [ true ] If the option is valid.
1676
- # @raise [ Error::InvalidMaxConnecting ] If the option is invalid.
1677
- def validate_max_connecting! ( option , opts )
1678
- if option == :max_connecting && opts . key? ( :max_connecting )
1679
- max_connecting = opts [ :max_connecting ] || Server ::ConnectionPool ::DEFAULT_MAX_CONNECTING
1680
- if max_connecting <= 0
1681
- raise Error :: InvalidMaxConnecting . new ( opts [ :max_connecting ] )
1682
- end
1696
+ def validate_max_connecting! ( opts )
1697
+ return unless opts . key? ( :max_connecting )
1698
+
1699
+ max_connecting = opts [ :max_connecting ] || Server ::ConnectionPool ::DEFAULT_MAX_CONNECTING
1700
+ if max_connecting <= 0
1701
+ opts [ :max_connecting ] = Server :: ConnectionPool :: DEFAULT_MAX_CONNECTING
1702
+ log_warn ( "Invalid max_connecting: #{ max_connecting } . Please ensure that it is greater than zero." )
1683
1703
end
1684
- true
1685
1704
end
1686
1705
1687
- def validate_read! ( option , opts )
1688
- if option == :read && opts . has_key? ( :read )
1689
- read = opts [ :read ]
1690
- # We could check if read is a Hash, but this would fail
1691
- # for custom classes implementing key access ([]).
1692
- # Instead reject common cases of strings and symbols.
1693
- if read . is_a? ( String ) || read . is_a? ( Symbol )
1694
- raise Error ::InvalidReadOption . new ( read , "the read preference must be specified as a hash: { mode: #{ read . inspect } }" )
1695
- end
1706
+ def validate_read! ( opts )
1707
+ return unless opts . has_key? ( :read )
1696
1708
1697
- if mode = read [ :mode ]
1698
- mode = mode . to_sym
1699
- unless Mongo ::ServerSelector ::PREFERENCES . include? ( mode )
1700
- raise Error ::InvalidReadOption . new ( read , "mode #{ mode } is not one of recognized modes" )
1701
- end
1709
+ read = opts [ :read ]
1710
+
1711
+ # We could check if read is a Hash, but this would fail
1712
+ # for custom classes implementing key access ([]).
1713
+ # Instead reject common cases of strings and symbols.
1714
+ if read . is_a? ( String ) || read . is_a? ( Symbol )
1715
+ raise Error ::InvalidReadOption . new ( read , "the read preference must be specified as a hash: { mode: #{ read . inspect } }" )
1716
+ end
1717
+
1718
+ if mode = read [ :mode ]
1719
+ mode = mode . to_sym
1720
+ unless Mongo ::ServerSelector ::PREFERENCES . include? ( mode )
1721
+ raise Error ::InvalidReadOption . new ( read , "mode #{ mode } is not one of recognized modes" )
1702
1722
end
1703
1723
end
1704
- true
1705
1724
end
1706
1725
1707
1726
def assert_not_closed
0 commit comments