From eb432489f190f3ecaacbdb858a54723d885662cc Mon Sep 17 00:00:00 2001 From: KaKa Date: Tue, 16 Feb 2021 19:09:49 +0800 Subject: [PATCH] fix: object schema extend extended schema --- src/FluentJSONSchema.d.ts | 2 +- src/types/index.ts | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/FluentJSONSchema.d.ts b/src/FluentJSONSchema.d.ts index e37fef1..77e42db 100644 --- a/src/FluentJSONSchema.d.ts +++ b/src/FluentJSONSchema.d.ts @@ -122,7 +122,7 @@ export interface ObjectSchema extends BaseSchema { patternProperties: (options: PatternPropertiesOptions) => ObjectSchema dependencies: (options: DependenciesOptions) => ObjectSchema propertyNames: (value: JSONSchema) => ObjectSchema - extend: (schema: ObjectSchema) => ExtendedSchema + extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema only: (properties: string[]) => ObjectSchema } diff --git a/src/types/index.ts b/src/types/index.ts index 85fd56a..d932cc9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -23,18 +23,12 @@ const schema = S.object() .prop('email2', S.string().format(S.FORMATS.EMAIL)) .prop( 'avatar', - S.string() - .contentEncoding('base64') - .contentMediaType('image/png') + S.string().contentEncoding('base64').contentMediaType('image/png') ) .required() .prop( 'password', - S.string() - .default('123456') - .minLength(6) - .maxLength(12) - .pattern('.*') + S.string().default('123456').minLength(6).maxLength(12).pattern('.*') ) .required() .prop('addresses', S.array().items([S.ref('#address')])) @@ -89,8 +83,10 @@ try { } } -const arrayExtendedSchema = S.array() - .items(userSchema) - .valueOf() +const arrayExtendedSchema = S.array().items(userSchema).valueOf() console.log('array of user\n', JSON.stringify(arrayExtendedSchema)) + +const extendExtendedSchema = S.object().extend(userSchema) + +console.log('extend of user\n', JSON.stringify(extendExtendedSchema))