-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for type safety when selecting fields and created a `fields` option which uses exact values.
- Loading branch information
Showing
16 changed files
with
355 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,117 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import cuid2Extension, { Cuid2ExtensionOptions } from "./cuid2-extension"; | ||
import { ExcludeField, Field, IncludeField } from "./valid-fields"; | ||
|
||
test("cuid2Extension returns an extension", () => { | ||
const extension = cuid2Extension(); | ||
expect(extension).toBeTypeOf("function"); | ||
}); | ||
|
||
test("cuid2Extension throws error when fields is not in correct format", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
fields: ["invalidFormat"] as unknown as Field[], | ||
}; | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot(` | ||
[ZodError: [ | ||
{ | ||
"validation": "regex", | ||
"code": "invalid_string", | ||
"message": "Invalid", | ||
"path": [ | ||
"fields", | ||
0 | ||
] | ||
} | ||
]] | ||
`); | ||
}); | ||
|
||
test("cuid2Extension throws error when using fields and includeFields", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
fields: ["Model:Field"] as unknown as Field[], | ||
includeFields: ["Model:Field"] as unknown as IncludeField[], | ||
}; | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot( | ||
`[Error: You cannot provide both \`fields\` and \`includeFields\`/\`excludeFields\` options.]`, | ||
); | ||
}); | ||
|
||
test("cuid2Extension throws error when includeFields is not provided", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
includeFields: undefined, | ||
}; | ||
expect(() => cuid2Extension(options)).toThrow("You must provide the `includeFields` option."); | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot(` | ||
[ZodError: [ | ||
{ | ||
"code": "invalid_type", | ||
"expected": "array", | ||
"received": "undefined", | ||
"path": [ | ||
"includeFields" | ||
], | ||
"message": "Required" | ||
} | ||
]] | ||
`); | ||
}); | ||
|
||
test("cuid2Extension throws error when includeFields is does not have at least on item", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
includeFields: [], | ||
}; | ||
expect(() => cuid2Extension(options)).toThrow("You must provide at least one field in the `includeFields` option."); | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot(` | ||
[ZodError: [ | ||
{ | ||
"code": "too_small", | ||
"minimum": 1, | ||
"type": "array", | ||
"inclusive": true, | ||
"exact": false, | ||
"message": "Array must contain at least 1 element(s)", | ||
"path": [ | ||
"includeFields" | ||
] | ||
} | ||
]] | ||
`); | ||
}); | ||
|
||
test("cuid2Extension throws error when includeFields is not in correct format", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
includeFields: ["invalidFormat"], | ||
includeFields: ["invalidFormat"] as unknown as IncludeField[], | ||
}; | ||
expect(() => cuid2Extension(options)).toThrow( | ||
"The `includeFields` option must be in the format of `ModelName:FieldName`.", | ||
); | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot(` | ||
[ZodError: [ | ||
{ | ||
"validation": "regex", | ||
"code": "invalid_string", | ||
"message": "Invalid", | ||
"path": [ | ||
"includeFields", | ||
0 | ||
] | ||
} | ||
]] | ||
`); | ||
}); | ||
|
||
test("cuid2Extension throws error when excludeFields is not in correct format", () => { | ||
const options: Cuid2ExtensionOptions = { | ||
includeFields: ["Model:Field"], | ||
excludeFields: ["invalidFormat"], | ||
includeFields: ["Model:Field"] as unknown as IncludeField[], | ||
excludeFields: ["invalidFormat"] as unknown as ExcludeField[], | ||
}; | ||
expect(() => cuid2Extension(options)).toThrow( | ||
"The `excludeFields` option must be in the format of `ModelName:FieldName`.", | ||
); | ||
expect(() => cuid2Extension(options)).toThrowErrorMatchingInlineSnapshot(` | ||
[ZodError: [ | ||
{ | ||
"validation": "regex", | ||
"code": "invalid_string", | ||
"message": "Invalid", | ||
"path": [ | ||
"excludeFields", | ||
0 | ||
] | ||
} | ||
]] | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import getExactFieldsFactory from "./get-exact-fields-factory"; | ||
|
||
test("getExactFieldsFactory returns a function", () => { | ||
const getFields = getExactFieldsFactory([]); | ||
expect(getFields).toBeTypeOf("function"); | ||
}); | ||
|
||
test("getExactFieldsFactory handles fields correctly", () => { | ||
const getFields = getExactFieldsFactory(["TestModel:field1", "TestModel:field2"]); | ||
const fields = getFields("TestModel"); | ||
expect(fields).toEqual(["field1", "field2"]); | ||
}); | ||
|
||
test("getExactFieldsFactory ignores fields for other models", () => { | ||
const getFields = getExactFieldsFactory(["TestModel:field1", "OtherModel:field2"]); | ||
const fields = getFields("TestModel"); | ||
expect(fields).toEqual(["field1"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { type GetFieldsFunction } from "./get-fields-function"; | ||
|
||
/** | ||
* Returns a function that returns the fields to apply to a model | ||
* | ||
* @param fields | ||
*/ | ||
export default function getExactFieldsFactory(fields: string[]): GetFieldsFunction { | ||
return (operationModel: string) => { | ||
return fields | ||
.filter((field) => { | ||
const [model] = field.split(":"); | ||
return model === operationModel; | ||
}) | ||
.map((fieldPair) => { | ||
const [, field] = fieldPair.split(":"); | ||
return field; | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Function that returns the fields on a model to apply the CUID2 extension | ||
*/ | ||
export type GetFieldsFunction = (operationModel: string) => string[]; |
Oops, something went wrong.