Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ One or more environment variables failed assertions: SESSION_STORE is not an
allowed value
```

It is also possible to define a regex that your environment variable should be.
```php
$dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})');
```

### Comments

You can comment your `.env` file using the `#` character. E.g.
Expand Down
20 changes: 20 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ function ($value) use ($choices) {
);
}

/**
* Assert that each variable matches the given regular expression
*
* @param string $regex
*
* @throws \Dotenv\Exception\ValidationException
*
* @return \Dotenv\Validator
*/
public function allowedRegexValues($regex)
{
return $this->assertCallback(
function ($value) use ($regex)
{
return preg_match($regex, $value) == 1 ;
},
sprintf('does not match given regex %s' , $regex)
);
}

/**
* Assert that the callback returns true for each variable.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Dotenv/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function testDotenvAllowedValues()
$this->assertTrue(true);
}

public function testDotenvallowedRegexValues()
{
$dotenv = Dotenv::create($this->fixturesFolder);
$dotenv->load();
$dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})');
$this->assertTrue(true);
}

public function testDotenvAllowedValuesIfPresent()
{
$dotenv = Dotenv::create($this->fixturesFolder);
Expand Down