-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (44 loc) · 1.77 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import strawberry
from typing import List
from fastapi import FastAPI
from strawberry.asgi import GraphQL
# Local import
from rules_password import *
@strawberry.type
class Response:
verify: bool
noMatch: List[str]
@strawberry.input
class Rules:
rule: str
value: int
@strawberry.type
class Query:
@strawberry.field
def verify(self, password: str, rules: List[Rules]) -> Response:
# Relates the rules available in GraphQL with the name of the functions in rules_password.py
dictFunctions = {
"minSize": min_size,
"minUppercase": min_upper_case,
"minLowercase": min_lower_case,
"minDigit": min_digit,
"minSpecialChars": min_special_chars,
"noRepeted": no_repeted,
}
# For each rule input in GraphQL, call the function and relate the name of the rule with the return to its function
dictResponse = {}
for ruleInput in rules:
dictResponse[ruleInput.rule] = dictFunctions[ruleInput.rule](password, ruleInput.value)
# Example: dictResponse["minSize"] = False (False it's return of min_size(string, value))
if all(list(dictResponse.values())):
# Returns empty "noMatch" attribute if all values in dictResponse are True
return Response(verify=True, noMatch=[""])
else:
# Return the rules that did not pass in the "noMatch" attribute
wrongChecks = [response for response in dictResponse if dictResponse[response] == False]
return Response(verify=False, noMatch=wrongChecks)
schema = strawberry.Schema(query=Query)
graphql_app = GraphQL(schema)
app = FastAPI()
app.add_route("/graphql", graphql_app)
app.add_websocket_route("/graphql", graphql_app)