-
Notifications
You must be signed in to change notification settings - Fork 0
feat: validate decorator rules #232
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
+259
−22
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
| import { FieldRuleFn } from './types' | ||
|
|
||
| const symbolWithoutDescription: FieldRuleFn = s => | ||
| typeof s.propertyKey === 'symbol' && !s.propertyKey.description ? `symbol property must have a description` : null | ||
|
|
||
| const emptySourceKey: FieldRuleFn = s => (s.hasSource && s.sourceKey === '' ? `@${s.sources[0].name} key must not be an empty string` : null) | ||
|
|
||
| export const BASIC_RULES: readonly FieldRuleFn[] = [symbolWithoutDescription, emptySourceKey] |
This file contains hidden or 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,25 @@ | ||
| import { With, Without } from '../validator' | ||
| import { FieldRuleFn, FieldState } from './types' | ||
|
|
||
| function unknownReference(state: FieldState, decoratorName: string): string | null { | ||
| for (const applied of state.appliedSelf) { | ||
| if (applied.name !== decoratorName) continue | ||
| const target = applied.args[0] | ||
| if (typeof target === 'string' && !state.siblingFields.has(target)) { | ||
| return target | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| const withReferencesUnknownField: FieldRuleFn = s => { | ||
| const target = unknownReference(s, With.name) | ||
| return target === null ? null : `@With references unknown field "${target}"` | ||
| } | ||
|
|
||
| const withoutReferencesUnknownField: FieldRuleFn = s => { | ||
| const target = unknownReference(s, Without.name) | ||
| return target === null ? null : `@Without references unknown field "${target}"` | ||
| } | ||
|
|
||
| export const CROSS_FIELD_RULES: readonly FieldRuleFn[] = [withReferencesUnknownField, withoutReferencesUnknownField] | ||
This file contains hidden or 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 hidden or 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,10 +1,19 @@ | ||
| import { makeFieldRuleChecker } from './ruleChecker' | ||
| import type { FieldRuleFn, RuleChecker } from './types' | ||
| import { BASIC_RULES } from './basic' | ||
| import { KIND_CATEGORY_RULES } from './kindCategory' | ||
| import { EACH_USAGE_RULES } from './eachUsage' | ||
| import { TYPE_HELPER_PLACEMENT_RULES } from './typeHelperPlacement' | ||
| import { CROSS_FIELD_RULES } from './crossField' | ||
|
|
||
| /** All field-level rule implementations currently active. */ | ||
| const FIELD_RULES: readonly FieldRuleFn[] = [...KIND_CATEGORY_RULES, ...EACH_USAGE_RULES] | ||
| const FIELD_RULES: readonly FieldRuleFn[] = [ | ||
| ...BASIC_RULES, | ||
| ...KIND_CATEGORY_RULES, | ||
| ...EACH_USAGE_RULES, | ||
| ...TYPE_HELPER_PLACEMENT_RULES, | ||
| ...CROSS_FIELD_RULES, | ||
| ] | ||
|
|
||
| /** Single RuleChecker that runs every active field rule. */ | ||
| export const ACTIVE_CHECKERS: readonly RuleChecker[] = [makeFieldRuleChecker(FIELD_RULES)] |
This file contains hidden or 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 hidden or 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,11 @@ | ||
| import { List, Type } from '../decorators' | ||
| import { isKnownNonArray, isPrimitiveType } from './utils' | ||
| import { FieldRuleFn } from './types' | ||
|
|
||
| const listOnNonArray: FieldRuleFn = s => | ||
| s.appliedSelf.some(d => d.name === List.name) && isKnownNonArray(s.fieldType) ? `@List can only be applied to array fields` : null | ||
|
|
||
| const typeOnPrimitive: FieldRuleFn = s => | ||
| s.appliedSelf.some(d => d.name === Type.name) && isPrimitiveType(s.fieldType) ? `@Type cannot be applied to a primitive field` : null | ||
|
|
||
| export const TYPE_HELPER_PLACEMENT_RULES: readonly FieldRuleFn[] = [listOnNonArray, typeOnPrimitive] |
This file contains hidden or 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 hidden or 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,9 @@ | ||
| const PRIMITIVE_TYPES: readonly unknown[] = [String, Number, Boolean] | ||
|
|
||
| export function isPrimitiveType(fieldType: unknown): boolean { | ||
| return PRIMITIVE_TYPES.includes(fieldType) | ||
| } | ||
|
|
||
| export function isKnownNonArray(fieldType: unknown): boolean { | ||
| return typeof fieldType === 'function' && fieldType !== Array && fieldType !== Object | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
| import { Body, CargoSchemaError } from '../../src' | ||
| import { expectViolation, validateCargoSchema } from './testUtils' | ||
|
|
||
| describe('schema validation — basic rules', () => { | ||
| it('rejects a symbol property without a description (A1)', () => { | ||
| const sym = Symbol() | ||
|
|
||
| class SymbolWithoutDescriptionDto { | ||
| @Body() | ||
| [sym]!: string | ||
| } | ||
|
|
||
| try { | ||
| validateCargoSchema(SymbolWithoutDescriptionDto) | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(CargoSchemaError) | ||
| expect((e as CargoSchemaError).violations.some(v => v.field === sym && v.message.includes('must have a description'))).toBe(true) | ||
| return | ||
| } | ||
| throw new Error('expected CargoSchemaError to be thrown') | ||
| }) | ||
|
|
||
| it('accepts a symbol property with a description', () => { | ||
| const sym = Symbol('userId') | ||
|
|
||
| class SymbolWithDescriptionDto { | ||
| @Body() | ||
| [sym]!: string | ||
| } | ||
|
|
||
| expect(() => validateCargoSchema(SymbolWithDescriptionDto)).not.toThrow() | ||
| }) | ||
|
|
||
| it('rejects an empty-string source key (A2)', () => { | ||
| class EmptyKeyDto { | ||
| @Body('') | ||
| foo!: string | ||
| } | ||
|
|
||
| expectViolation(() => validateCargoSchema(EmptyKeyDto), 'foo', 'key must not be an empty string') | ||
| }) | ||
|
|
||
| it('accepts @Body() without an explicit key', () => { | ||
| class DefaultKeyDto { | ||
| @Body() | ||
| foo!: string | ||
| } | ||
|
|
||
| expect(() => validateCargoSchema(DefaultKeyDto)).not.toThrow() | ||
| }) | ||
| }) |
This file contains hidden or 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,41 @@ | ||
| import { Body, With, Without } from '../../src' | ||
| import { expectViolation, validateCargoSchema } from './testUtils' | ||
|
|
||
| describe('schema validation — cross-field reference rules', () => { | ||
| it('rejects @With referencing a non-existent field (G1)', () => { | ||
| class WithUnknownDto { | ||
| @Body() | ||
| @With('ghost') | ||
| foo!: string | ||
| } | ||
|
|
||
| expectViolation(() => validateCargoSchema(WithUnknownDto), 'foo', '@With references unknown field "ghost"') | ||
| }) | ||
|
|
||
| it('rejects @Without referencing a non-existent field (G2)', () => { | ||
| class WithoutUnknownDto { | ||
| @Body() | ||
| @Without('ghost') | ||
| foo!: string | ||
| } | ||
|
|
||
| expectViolation(() => validateCargoSchema(WithoutUnknownDto), 'foo', '@Without references unknown field "ghost"') | ||
| }) | ||
|
|
||
| it('accepts references to existing fields', () => { | ||
| class CrossFieldValidDto { | ||
| @Body() | ||
| bar!: string | ||
|
|
||
| @Body() | ||
| @With('bar') | ||
| @Without('baz') | ||
| foo!: string | ||
|
|
||
| @Body() | ||
| baz!: string | ||
| } | ||
|
|
||
| expect(() => validateCargoSchema(CrossFieldValidDto)).not.toThrow() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.