Skip to content

Commit

Permalink
Do not add non-existent relationship properties (#90)
Browse files Browse the repository at this point in the history
- Fix issue where the relationship property would be added even if it
was optional.
- Make schema validation "required" property non-optional.
  • Loading branch information
themetalfleece authored Jan 15, 2024
1 parent c05c4b5 commit 84992f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions documentation/md/docs/Models/Defining-a-Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
> > },
> > },
> > }
Expand Down Expand Up @@ -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,
> > },
> > },
> > }
Expand Down
24 changes: 23 additions & 1 deletion src/ModelOps/ModelOps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('ModelFactory', () => {
age: {
type: 'number',
minimum: 0,
required: false,
},
id: {
type: 'string',
Expand All @@ -157,6 +158,7 @@ describe('ModelFactory', () => {
type: 'number',
minimum: 1,
maximum: 5,
required: true,
},
},
},
Expand Down Expand Up @@ -424,6 +426,7 @@ describe('createOne', () => {
property: 'rating',
schema: {
type: 'number',
required: true,
},
},
},
Expand Down Expand Up @@ -818,6 +821,7 @@ describe('addRelationships', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand All @@ -840,6 +844,7 @@ describe('addRelationships', () => {
property: 'more',
schema: {
type: 'boolean',
required: true,
},
},
},
Expand Down Expand Up @@ -1260,6 +1265,7 @@ describe('beforeCreate', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand Down Expand Up @@ -1571,7 +1577,13 @@ describe('relateTo', () => {
interface UsersRelatedNodesI {
Parent: ModelRelatedNodesI<
{ createOne: (typeof Users)['createOne'] },
UsersInstance
UsersInstance,
{
Rating?: number;
},
{
rating?: number;
}
>;
}

Expand Down Expand Up @@ -1614,6 +1626,15 @@ describe('relateTo', () => {
model: 'self',
direction: 'out',
name: 'PARENT',
properties: {
Rating: {
property: 'rating',
schema: {
type: 'number',
required: false,
},
},
},
},
},
primaryKeyField: 'id',
Expand Down Expand Up @@ -1704,6 +1725,7 @@ describe('relateTo', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand Down
24 changes: 14 additions & 10 deletions src/ModelOps/ModelOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type RelationshipTypePropertyForCreateI<
| Array<RelationshipTypePropertyForCreateWhereI<RelationshipProperties>>;
};

type IValidationSchema<T = AnyObject> = Revalidator.ISchema<T> & {
required: boolean;
};

/** the type for the Relationship configuration of a Model */
export type RelationshipsI<RelatedNodesToAssociateI extends AnyObject> = {
/** the alias of the relationship definitions is the key */
Expand All @@ -118,7 +122,7 @@ export type RelationshipsI<RelatedNodesToAssociateI extends AnyObject> = {
/** the actual property to be used on the relationship */
property: keyof RelatedNodesToAssociateI[alias]['RelationshipProperties'];
/** validation for the property */
schema: Revalidator.ISchema<AnyObject>;
schema: IValidationSchema;
};
};
};
Expand Down Expand Up @@ -416,7 +420,7 @@ export const ModelFactory = <
/** the schema for the validation */
schema: {
[index in keyof Properties]:
| Revalidator.ISchema<Properties>
| IValidationSchema<Properties>
| Revalidator.JSONSchema<Properties>;
};
/** the label of the nodes */
Expand Down Expand Up @@ -1689,24 +1693,24 @@ export const ModelFactory = <
/** properties to be used in the relationship */
const relationshipProperties = {};
/** total validation for the properties */
const validationSchema: Record<
string,
Revalidator.ISchema<AnyObject>
> = {};
const validationSchema: Record<string, IValidationSchema<AnyObject>> = {};

for (const key of keysToUse) {
const property = relationship.properties?.[key]?.property as string;

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, {
Expand Down

0 comments on commit 84992f9

Please sign in to comment.