Skip to content

Commit

Permalink
add part of a validate() test
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Mar 27, 2022
1 parent c238368 commit b367816
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/validated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class ValidatedChangeset {
let obj = this[ERRORS];
let original = this[CONTENT];

return newFormat(getKeyValues(obj), original, this.getDeep);
return getKeyErrorValues(obj);
}

get change() {
Expand Down Expand Up @@ -395,8 +395,9 @@ export class ValidatedChangeset {
*/
async validate(): Promise<any> {
const changes = this[CHANGES];
const content = this[CONTENT];

return this.Validator.validate(normalizeObject(changes));
return this.Validator.validate({ ...normalizeObject(content), ...normalizeObject(changes) });
}

/**
Expand Down Expand Up @@ -427,7 +428,7 @@ export class ValidatedChangeset {
this[ERRORS_CACHE] = this[ERRORS];

// Return passed-in `error`.
return error;
return newError;
}

/**
Expand Down
69 changes: 43 additions & 26 deletions test/validated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ let userSchema = object({
name: string().required(),
age: number().required().positive().integer(),
email: string().email(),
org: object({
usa: object({
minAge: number().moreThan(18)
}),
}),
teams: array(string()),
website: string().url().nullable(),
createdOn: date().default(() => new Date()),
Expand Down Expand Up @@ -2435,34 +2440,46 @@ describe('Unit | Utility | validation changeset', () => {
* #validate
*/

// it.only('#validate/0 validates all fields immediately', async () => {
// dummyModel.name = 'J';
// dummyModel.age = 10;
// dummyModel.password = false;
// dummyModel.options = null;
// let dummyChangeset = Changeset(dummyModel, userSchema);

// await dummyChangeset.validate();
// expect(get(dummyChangeset, 'error.password')).toEqual({
// validation: ['foo', 'bar'],
// value: false
// });
// expect(dummyChangeset.changes).toEqual({});
// expect(get(dummyChangeset, 'errors.length')).toBe(8);
// });
it('#validate/0', async () => {
expect.assertions(5);
dummyModel.name = 'J';
let dummyChangeset = Changeset(dummyModel, userSchema);

try {
await dummyChangeset.validate();
} catch (e) {
expect(e.message).toEqual('age is a required field');
const error = dummyChangeset.addError('age', e.message);
expect(get(dummyChangeset, 'error.age')).toEqual(error);
}
expect(dummyChangeset.changes).toEqual({});
expect(get(dummyChangeset, 'errors.length')).toBe(1);
expect(get(dummyChangeset, 'errors')).toEqual([
{ key: 'age', validation: 'age is a required field', value: undefined }
]);
});

// it('#validate/0 validates nested fields', async () => {
// dummyModel.org = { usa: { ny: 7 } };
// let dummyChangeset = Changeset(dummyModel, userSchema), dummyValidations);
it('#validate/0 validates nested fields', async () => {
expect.assertions(5);
dummyModel.name = 'scott';
dummyModel.age = 10;
dummyModel.org = { usa: { minAge: 7 } };
let dummyChangeset = Changeset(dummyModel, userSchema);

try {
await dummyChangeset.validate();
} catch (e) {
expect(e.message).toEqual('org.usa.minAge must be greater than 18');
const error = dummyChangeset.addError('org.usa.minAge', e.message);
expect(get(dummyChangeset, 'error.org.usa.minAge')).toEqual(error);
}

// await dummyChangeset.validate();
// expect(get(dummyChangeset, 'error.org.usa.ny')).toEqual({
// validation: ['only letters work'],
// value: 7
// });
// expect(dummyChangeset.changes).toEqual([]);
// expect(get(dummyChangeset, 'errors.length')).toBe(8);
// });
expect(dummyChangeset.changes).toEqual({});
expect(get(dummyChangeset, 'errors.length')).toBe(1);
expect(get(dummyChangeset, 'errors')).toEqual([
{ key: 'org.usa.minAge', validation: 'org.usa.minAge must be greater than 18', value: 7 }
]);
});

// it('#validate/1 validates a single field immediately', async () => {
// dummyModel.name = 'J';
Expand Down

0 comments on commit b367816

Please sign in to comment.