-
Notifications
You must be signed in to change notification settings - Fork 0
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
refactor: Generator parts from interfaces into zod objects #42
Changes from all commits
b20692a
ab55743
79bd8f7
bc54ae9
5abcd9f
fbbb868
132e39b
3396159
8e36198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from 'zod' | ||
import { TestRule } from '@/schemas/rules' | ||
import { TestData } from '@/schemas/testData' | ||
import { TestOptions } from '@/schemas/testOptions' | ||
|
||
export const GeneratorFileData = z.object({ | ||
name: z.string(), | ||
version: z.string(), | ||
recordingPath: z.string(), | ||
options: TestOptions, | ||
testData: TestData, | ||
rules: TestRule.array(), | ||
allowlist: z.string().array(), | ||
}) | ||
|
||
export type GeneratorFileData = z.infer<typeof GeneratorFileData> | ||
Comment on lines
+6
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know there are no conflicts between exported type and const of the same name, but I would still prefer zod objects to be appended by Schema to be more explicit: export const GeneratorFileDataSchema = z.object({ .. })
export type GeneratorFileData = z.infer<typeof GeneratorFileData> WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I like that the names are shared so depending on where you use it you get the right "object" 🤔 In theory you know that it's a schema because it will be imported from It's also true that I come from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a bit magical and I was surprised to learn TypeScript doesn't throw an error in such cases. I think I agree with Edgar that we should append import { TestRuleSchema } from '@schemas/rules.ts'
export type TestRuleSchema = z.infer<typeof TestRuleSchema> This way we'll end up keeping schemas and types in separate folders and will be importing types from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense, once you "compile" the code types are stripped out so if you are importing and using something it's the actual object. I have a preference for that approach probably because I'm coming from python and it's less verbose but if the majority of the team thinks that separating schemas & types aids with readability, I think it's worth the cost of a few more lines 🙌 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { z } from 'zod' | ||
|
||
export const VariableValue = z.object({ | ||
type: z.literal('variable'), | ||
variableName: z.string(), | ||
}) | ||
|
||
export const ArrayValue = z.object({ | ||
type: z.literal('array'), | ||
arrayName: z.string(), | ||
}) | ||
|
||
export const CustomCodeValue = z.object({ | ||
type: z.literal('customCode'), | ||
getValue: z | ||
.function() | ||
.returns(z.union([z.string(), z.number(), z.null(), z.void()])), | ||
}) | ||
|
||
export const RecordedValue = z.object({ | ||
type: z.literal('recordedValue'), | ||
}) | ||
|
||
export const Filter = z.object({ | ||
path: z.string(), | ||
}) | ||
|
||
export const BeginEndSelector = z.object({ | ||
type: z.literal('begin-end'), | ||
from: z.enum(['headers', 'body', 'url']), | ||
begin: z.string(), | ||
end: z.string(), | ||
}) | ||
|
||
export const RegexSelector = z.object({ | ||
type: z.literal('regex'), | ||
from: z.enum(['headers', 'body', 'url']), | ||
regex: z.string(), | ||
}) | ||
|
||
export const JsonSelector = z.object({ | ||
type: z.literal('json'), | ||
from: z.literal('body'), | ||
path: z.string(), | ||
}) | ||
|
||
export const CustomCodeSelector = z.object({ | ||
type: z.literal('custom-code'), | ||
snippet: z.string(), | ||
}) | ||
|
||
export const Selector = z.discriminatedUnion('type', [ | ||
BeginEndSelector, | ||
RegexSelector, | ||
JsonSelector, | ||
]) | ||
|
||
export const CorrelationExtractor = z.object({ | ||
filter: Filter, | ||
selector: Selector, | ||
variableName: z.string().optional(), | ||
}) | ||
|
||
export const CorrelationReplacer = z.object({ | ||
filter: Filter, | ||
selector: Selector, | ||
}) | ||
|
||
export const RuleBase = z.object({ | ||
id: z.string(), | ||
}) | ||
|
||
export const ParameterizationRule = RuleBase.extend({ | ||
type: z.literal('parameterization'), | ||
filter: Filter, | ||
selector: Selector, | ||
value: z.discriminatedUnion('type', [ | ||
VariableValue, | ||
ArrayValue, | ||
CustomCodeValue, | ||
]), | ||
}) | ||
|
||
export const CorrelationRule = RuleBase.extend({ | ||
type: z.literal('correlation'), | ||
extractor: CorrelationExtractor, | ||
replacer: CorrelationReplacer.optional(), | ||
}) | ||
|
||
export const VerificationRule = RuleBase.extend({ | ||
type: z.literal('verification'), | ||
filter: Filter, | ||
selector: Selector, | ||
value: z.discriminatedUnion('type', [ | ||
VariableValue, | ||
ArrayValue, | ||
CustomCodeValue, | ||
RecordedValue, | ||
]), | ||
}) | ||
|
||
export const CustomCodeRule = RuleBase.extend({ | ||
type: z.literal('customCode'), | ||
filter: Filter, | ||
placement: z.enum(['before', 'after']), | ||
snippet: z.string(), | ||
}) | ||
|
||
export const TestRule = z.discriminatedUnion('type', [ | ||
ParameterizationRule, | ||
CorrelationRule, | ||
VerificationRule, | ||
CustomCodeRule, | ||
]) | ||
|
||
export type VariableValue = z.infer<typeof VariableValue> | ||
export type ArrayValue = z.infer<typeof ArrayValue> | ||
export type CustomCodeValue = z.infer<typeof CustomCodeValue> | ||
export type RecordedValue = z.infer<typeof RecordedValue> | ||
export type Filter = z.infer<typeof Filter> | ||
export type BeginEndSelector = z.infer<typeof BeginEndSelector> | ||
export type RegexSelector = z.infer<typeof RegexSelector> | ||
export type JsonSelector = z.infer<typeof JsonSelector> | ||
export type CustomCodeSelector = z.infer<typeof CustomCodeSelector> | ||
export type Selector = z.infer<typeof Selector> | ||
export type CorrelationExtractor = z.infer<typeof CorrelationExtractor> | ||
export type CorrelationReplacer = z.infer<typeof CorrelationReplacer> | ||
export type RuleBase = z.infer<typeof RuleBase> | ||
export type ParameterizationRule = z.infer<typeof ParameterizationRule> | ||
export type CorrelationRule = z.infer<typeof CorrelationRule> | ||
export type VerificationRule = z.infer<typeof VerificationRule> | ||
export type CustomCodeRule = z.infer<typeof CustomCodeRule> | ||
export type TestRule = z.infer<typeof TestRule> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from 'zod' | ||
|
||
export const Variable = z.object({ | ||
name: z.string(), | ||
value: z.string(), | ||
}) | ||
|
||
export const TestData = z.object({ | ||
variables: Variable.array(), | ||
}) | ||
|
||
export type Variable = z.infer<typeof Variable> | ||
export type TestData = z.infer<typeof TestData> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either messaging between render and main OR do the parsing on render side and let it handle there 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I left the comment there! 😄
Regardless of leveraging it in here or not, that system would be useful for having a channel between main/renderer to just send errors on any operations, the item has already been added on the todo list.
In the future we might move more operations on the main process, so handling the parsing on the renderer side would just be probably a temporary solution 🤔