-
Notifications
You must be signed in to change notification settings - Fork 100
Add Standard Schema validation support for configuration schema #2122
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
Open
iamnivekx
wants to merge
6
commits into
nestjs:master
Choose a base branch
from
nestjs-labs:feature/extend-schema
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dab84cf
feat: add Zod validation support for configuration schema
iamnivekx b93de26
feat: introduce ZodType type and update validation schema methods for…
iamnivekx dbe454d
feat: integrate Standard Schema validation
iamnivekx 214f44b
refactor: simplify StandardValidator constructor and remove unused op…
iamnivekx 5dbb732
refactor: update validation schema documentation and improve JoiValid…
iamnivekx 3cd96b3
refactor: enhance validation logic and improve schema detection in Va…
iamnivekx 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
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,3 +1,4 @@ | ||
| export * from './config-change-event.interface'; | ||
| export * from './config-factory.interface'; | ||
| export * from './config-module-options.interface'; | ||
| export * from './validation-schema.interface'; |
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,39 @@ | ||
| import { type StandardSchemaV1 } from '@standard-schema/spec'; | ||
| import { type Schema as JoiSchema } from 'joi'; | ||
|
|
||
| /** | ||
| * @publicApi | ||
| */ | ||
| export { StandardSchemaV1 }; | ||
|
|
||
| /** | ||
| * @publicApi | ||
| */ | ||
| export { JoiSchema }; | ||
|
|
||
| /** | ||
| * @publicApi | ||
| */ | ||
| export type ValidationSchema = JoiSchema | StandardSchemaV1; | ||
|
|
||
| /** | ||
| * @publicApi | ||
| */ | ||
| export interface ValidationOptions { | ||
| /** | ||
| * [Joi] Whether to allow unknown properties | ||
| * @default true | ||
| */ | ||
| allowUnknown?: boolean; | ||
|
|
||
| /** | ||
| * [Joi] Whether to abort validation on first error | ||
| * @default false | ||
| */ | ||
| abortEarly?: boolean; | ||
|
|
||
| /** | ||
| * Additional validation options specific to the validation library | ||
| */ | ||
| [key: string]: any; | ||
| } |
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,36 @@ | ||
| /** | ||
| * @publicApi | ||
| */ | ||
| export abstract class Validator { | ||
| validate( | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| _config: Record<string, any>, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| _options?: Record<string, any>, | ||
| ): { error?: Error; value: Record<string, any> } { | ||
| throw new Error('Please implement the validate method'); | ||
| } | ||
|
|
||
| succeed(result: unknown): { | ||
| error?: Error; | ||
| value: Record<string, any>; | ||
| } { | ||
| return { | ||
| value: result as Record<string, any>, | ||
| error: undefined, | ||
| }; | ||
| } | ||
|
|
||
| failed( | ||
| error: Error, | ||
| config: Record<string, any>, | ||
| ): { | ||
| error: Error; | ||
| value: Record<string, any>; | ||
| } { | ||
| return { | ||
| error, | ||
| value: config, | ||
| }; | ||
| } | ||
| } |
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,4 @@ | ||
| export * from './abstract.validator'; | ||
| export * from './joi.validator'; | ||
| export * from './standard.validator'; | ||
| export * from './validator.factory'; |
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,28 @@ | ||
| import type { | ||
| ValidationOptions, | ||
| JoiSchema, | ||
| } from '../interfaces/validation-schema.interface'; | ||
| import { Validator } from './abstract.validator'; | ||
|
|
||
| /** | ||
| * Joi validation schema adapter | ||
| * @see https://joi.dev/api | ||
| * @publicApi | ||
| */ | ||
| export class JoiValidator extends Validator { | ||
| constructor(private schema: JoiSchema) { | ||
| super(); | ||
| } | ||
|
|
||
| validate( | ||
| config: Record<string, any>, | ||
| validationOptions?: ValidationOptions, | ||
| ): { error?: Error; value: Record<string, any> } { | ||
| const { error, value } = this.schema.validate(config, validationOptions); | ||
| if (error) { | ||
| return this.failed(error, config); | ||
| } | ||
|
|
||
| return this.succeed(value); | ||
| } | ||
| } |
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,31 @@ | ||
| import type { StandardSchemaV1 } from '@standard-schema/spec'; | ||
| import { Validator } from './abstract.validator'; | ||
|
|
||
| /** | ||
| * Standard Schema adapter | ||
| * @see https://standardschema.dev/ | ||
| * @publicApi | ||
| */ | ||
| export class StandardValidator extends Validator { | ||
| constructor(private schema: StandardSchemaV1<any, any>) { | ||
| super(); | ||
| } | ||
|
|
||
| validate(config: StandardSchemaV1): { | ||
| error?: Error; | ||
| value: Record<string, any>; | ||
| } { | ||
| const result = this.schema['~standard'].validate(config); | ||
| if (result instanceof Promise) { | ||
| throw new Error('Expected sync result'); | ||
| } | ||
| if ('value' in result) { | ||
| return this.succeed(result.value); | ||
| } | ||
|
|
||
| return this.failed( | ||
| new Error(JSON.stringify(result.issues, null, 2)), | ||
| config, | ||
| ); | ||
| } | ||
| } |
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,63 @@ | ||
| import type { | ||
| StandardSchemaV1, | ||
| ValidationSchema, | ||
| JoiSchema, | ||
| } from '../interfaces/validation-schema.interface'; | ||
| import { Validator } from './abstract.validator'; | ||
| import { JoiValidator } from './joi.validator'; | ||
| import { StandardValidator } from './standard.validator'; | ||
|
|
||
| /** | ||
| * Factory for creating validation schemas | ||
| * @publicApi | ||
| */ | ||
| export class ValidatorFactory { | ||
| /** | ||
| * Creates a Joi validator | ||
| * @param schema Joi schema | ||
| * @returns JoiValidator instance | ||
| */ | ||
| static createJoiValidator(schema: JoiSchema): Validator { | ||
| return new JoiValidator(schema); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a standard schema validator | ||
| * @param schema Standard schema | ||
| * @returns StandardValidator instance | ||
| */ | ||
| static createStandardValidator(schema: StandardSchemaV1): Validator { | ||
| return new StandardValidator(schema); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a validator from a schema object | ||
| * Automatically detects the schema type based on the schema object | ||
| * @param schema Schema object (Joi or a schema that conforms to @standard-schema/spec) | ||
| * @returns Validator instance | ||
| */ | ||
| static createValidator(schema: ValidationSchema): Validator { | ||
| if (schema instanceof Validator) { | ||
| return schema; | ||
| } | ||
|
|
||
| // Detect Joi schema by checking for the Joi-specific symbol | ||
| // Joi schemas have a special symbol that identifies them as Joi schemas | ||
| // Reference: https://github.com/hapijs/joi/blob/1b923c1336fb3957733b920a8290c2e2ac68dc88/lib/common.js#L124 | ||
| if (schema && !!(schema as any)[Symbol.for('@hapi/joi/schema')]) { | ||
| return this.createJoiValidator(schema as JoiSchema); | ||
| } | ||
|
|
||
| // Detect Standard Schema by checking for the '~standard' property | ||
| // Standard schemas conform to the @standard-schema/spec specification | ||
| // and contain a '~standard' property with validation logic | ||
| if (schema && typeof schema === 'object' && '~standard' in schema) { | ||
| return this.createStandardValidator(schema as StandardSchemaV1); | ||
| } | ||
|
|
||
| // If no valid schema type is detected, throw an error | ||
| throw new Error( | ||
| 'Unsupported schema type. Please use Joi schema, Standard Schema, or implement a custom validator.', | ||
| ); | ||
| } | ||
| } | ||
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.
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.
why not put these factory methods in validators themselves?