Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a class that validates whether the keys required by the application exist in the current environment #42

Closed
MrDave1999 opened this issue Feb 20, 2022 · 0 comments · Fixed by #43
Labels
feature New feature or request
Milestone

Comments

@MrDave1999
Copy link
Owner

MrDave1999 commented Feb 20, 2022

The idea of this new feature is to be able to request that certain keys are required by the application, in case they do not exist in the current environment, an error is generated.

Example:

new EnvLoader().Load();
new EnvValidator()
       .SetRequiredKeys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
       .Validate();

In the above example we first load the .env file and set the variables, then we call the Validate method to validate if the required keys exist in the current environment, otherwise an exception is thrown.

In other words, after calling the Load method, it will check if the keys "SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET" exist in the environment of the current process.

You can also ignore the exception and handle the error in another way:

new EnvLoader().Load();
new EnvValidator()
       .IgnoreException()
       .SetRequiredKeys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
       .Validate(out EnvValidationResult result);

Console.WriteLine(result.ErrorMessages);

You can also represent the required keys in a class and use each property as a key, for example:

class RequiredKeys
{
    public string SERVICE_APP_ID { get; }
    public string SERVICE_KEY { get; }
    public string SERVICE_SECRET { get; }
}

new EnvLoader().Load();
new EnvValidator()
       .IgnoreException()
       .SetRequiredKeys<RequiredKeys>()
       .Validate(out EnvValidationResult result);

Console.WriteLine(result.ErrorMessages);
@MrDave1999 MrDave1999 added the feature New feature or request label Feb 20, 2022
@MrDave1999 MrDave1999 changed the title Add support for requesting required keys Add support for requesting certain keys to be required by the application Feb 20, 2022
@MrDave1999 MrDave1999 changed the title Add support for requesting certain keys to be required by the application Add a class that validates whether the keys required by the application exist in the current environment Feb 22, 2022
MrDave1999 added a commit that referenced this issue Feb 24, 2022
…tion exist in the current environment (issue #42)
MrDave1999 added a commit that referenced this issue Feb 24, 2022
@MrDave1999 MrDave1999 reopened this Mar 2, 2022
@MrDave1999 MrDave1999 changed the title Add a class that validates whether the keys required by the application exist in the current environment Add a interface that validates whether the keys required by the application exist in the current environment Mar 2, 2022
@MrDave1999 MrDave1999 changed the title Add a interface that validates whether the keys required by the application exist in the current environment Add a class that validates whether the keys required by the application exist in the current environment Mar 6, 2022
MrDave1999 added a commit that referenced this issue Mar 6, 2022
MrDave1999 added a commit that referenced this issue Mar 16, 2022
@MrDave1999 MrDave1999 added this to the 8.0.0 milestone Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
1 participant