diff --git a/documentation/md/docs/Models/Defining-a-Model.md b/documentation/md/docs/Models/Defining-a-Model.md index f93e1c2..179e05c 100644 --- a/documentation/md/docs/Models/Defining-a-Model.md +++ b/documentation/md/docs/Models/Defining-a-Model.md @@ -62,6 +62,7 @@ A sample Model definition with all configuration options is the following. Note > > /* --> schema validation for it */ > > schema: { > > type: 'number', +> > required: true, > > }, > > }, > > } @@ -186,6 +187,7 @@ A sample Model definition with all configuration options is the following. Note > > /* --> schema validation for it */ > > schema: { > > type: 'number', +> > required: true, > > }, > > }, > > } diff --git a/src/ModelOps/ModelOps.spec.ts b/src/ModelOps/ModelOps.spec.ts index 3300e2a..9932807 100644 --- a/src/ModelOps/ModelOps.spec.ts +++ b/src/ModelOps/ModelOps.spec.ts @@ -139,6 +139,7 @@ describe('ModelFactory', () => { age: { type: 'number', minimum: 0, + required: false, }, id: { type: 'string', @@ -157,6 +158,7 @@ describe('ModelFactory', () => { type: 'number', minimum: 1, maximum: 5, + required: true, }, }, }, @@ -424,6 +426,7 @@ describe('createOne', () => { property: 'rating', schema: { type: 'number', + required: true, }, }, }, @@ -818,6 +821,7 @@ describe('addRelationships', () => { type: 'number', minimum: 0, maximum: 5, + required: true, }, }, }, @@ -840,6 +844,7 @@ describe('addRelationships', () => { property: 'more', schema: { type: 'boolean', + required: true, }, }, }, @@ -1260,6 +1265,7 @@ describe('beforeCreate', () => { type: 'number', minimum: 0, maximum: 5, + required: true, }, }, }, @@ -1571,7 +1577,13 @@ describe('relateTo', () => { interface UsersRelatedNodesI { Parent: ModelRelatedNodesI< { createOne: (typeof Users)['createOne'] }, - UsersInstance + UsersInstance, + { + Rating?: number; + }, + { + rating?: number; + } >; } @@ -1614,6 +1626,15 @@ describe('relateTo', () => { model: 'self', direction: 'out', name: 'PARENT', + properties: { + Rating: { + property: 'rating', + schema: { + type: 'number', + required: false, + }, + }, + }, }, }, primaryKeyField: 'id', @@ -1704,6 +1725,7 @@ describe('relateTo', () => { type: 'number', minimum: 0, maximum: 5, + required: true, }, }, }, diff --git a/src/ModelOps/ModelOps.ts b/src/ModelOps/ModelOps.ts index 92cf947..13446b2 100644 --- a/src/ModelOps/ModelOps.ts +++ b/src/ModelOps/ModelOps.ts @@ -101,6 +101,10 @@ type RelationshipTypePropertyForCreateI< | Array>; }; +type IValidationSchema = Revalidator.ISchema & { + required: boolean; +}; + /** the type for the Relationship configuration of a Model */ export type RelationshipsI = { /** the alias of the relationship definitions is the key */ @@ -118,7 +122,7 @@ export type RelationshipsI = { /** the actual property to be used on the relationship */ property: keyof RelatedNodesToAssociateI[alias]['RelationshipProperties']; /** validation for the property */ - schema: Revalidator.ISchema; + schema: IValidationSchema; }; }; }; @@ -416,7 +420,7 @@ export const ModelFactory = < /** the schema for the validation */ schema: { [index in keyof Properties]: - | Revalidator.ISchema + | IValidationSchema | Revalidator.JSONSchema; }; /** the label of the nodes */ @@ -1689,10 +1693,7 @@ export const ModelFactory = < /** properties to be used in the relationship */ const relationshipProperties = {}; /** total validation for the properties */ - const validationSchema: Record< - string, - Revalidator.ISchema - > = {}; + const validationSchema: Record> = {}; for (const key of keysToUse) { const property = relationship.properties?.[key]?.property as string; @@ -1700,13 +1701,16 @@ export const ModelFactory = < if (!property) { continue; } - relationshipProperties[property] = dataToUse[key]; const schema = relationship.properties?.[key]?.schema; - if (!schema) { - continue; + + if (schema) { + validationSchema[property] = schema; + } + + if (key in dataToUse) { + relationshipProperties[property] = dataToUse[key]; } - validationSchema[property] = schema; } const validationResult = revalidator.validate(relationshipProperties, {