Skip to content

Commit

Permalink
fixs #164
Browse files Browse the repository at this point in the history
  • Loading branch information
moshest committed Mar 26, 2022
1 parent 5bdbfc2 commit faac9ee
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
18 changes: 9 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"extends": [
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",

// Make sure this is always the last configuration in the extends array:
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true
},
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"no-console": ["error", { "allow": ["info", "warn", "error"] }],
"no-warning-comments": ["error", { "terms": ["fixme"], "location": "start" }]
"@typescript-eslint/no-unused-vars": "warn",
"no-console": ["warn", { "allow": ["info", "warn", "error"] }],
"no-warning-comments": ["warn", { "terms": ["fixme"], "location": "start" }]
}
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ typings/

# typescript compiled files
lib/

.history/
.DS_Store
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".js,.ts\" --source-maps inline",
"lint": "eslint \"src/**\"",
"lint:strict": "npm run lint -- --max-warnings 0",
"fix": "npm run fix:lint",
"fix:lint": "npm run lint -- --fix",
"mocha": "TS_NODE_FILES=true mocha -r ts-node/register \"src/**/*.test.ts\" --timeout 10000",
Expand All @@ -34,7 +35,7 @@
"test:build": "npm run mocha:build",
"test:commit": "if [[ -z \"$(git status --untracked-files=no --porcelain)\" ]]; then\n echo \"All filed committed.\"\nelse\n echo \"Uncommitted changes found. Please Commit them first.\" && exit 1\nfi",
"prepare": "npm run clean && npm run build && husky install",
"prepublishOnly": "npm run test:commit && npm run test:build",
"prepublishOnly": "npm run test:commit && npm run test:build && npm run lint:strict",
"postpublish": "git push && git push --tags",
"postversion": "npm publish"
},
Expand Down
36 changes: 36 additions & 0 deletions src/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ describe('Schema', () => {
assert.equal(error.message, 'username: my error');
}
});

it('should handle nested objects', () => {
const validator = Schema({
foo: Schema({
bar: string.min(1),
}),
});

typeCheck<
typeof validator,
(x: { foo: { bar: string } }) => { foo: { bar: string } }
>('ok');
assert.deepEqual(validator({ foo: { bar: 'abc' } }), {
foo: { bar: 'abc' },
});

try {
validator({ foo: { bar: '' } } as never);
assert.fail('should throw');
} catch (e: any) {
assert.instanceOf(e, ValidationError);

assert.equal(
e.message,
'foo.bar: Expect length to be minimum of 1 characters (actual: 0)',
);
assert.isArray(e.errors);

assert.instanceOf(e.errors[0].error, RangeError);
assert.equal(
e.errors[0].error.message,
'Expect length to be minimum of 1 characters (actual: 0)',
);
assert.deepEqual(e.errors[0].path, ['foo', 'bar']);
}
});
});

describe('.either', () => {
Expand Down
30 changes: 30 additions & 0 deletions src/schema/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,36 @@ describe('schema', () => {
}
});

it('nested error path', () => {
const validator = compiler({
foo: {
bar: (input: string) => {
if (typeof input !== 'string') {
throw new RangeError('my range error');
}

return input.toUpperCase();
},
},
});

assert.deepEqual(validator({ foo: { bar: 'bar' } }), {
foo: { bar: 'BAR' },
});

try {
validator({ foo: { bar: 1 } as never });
assert.fail('should throw');
} catch (e: any) {
assert.equal(e.message, 'foo.bar: my range error');
assert.isArray(e.errors);

assert.instanceOf(e.errors[0].error, RangeError);
assert.equal(e.errors[0].error.message, 'my range error');
assert.deepEqual(e.errors[0].path, ['foo', 'bar']);
}
});

it('error change path', () => {
const validator = compiler({
foo: {
Expand Down
2 changes: 1 addition & 1 deletion src/schema/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function compiler<S>(
const tasks = keys.map((key): SchemaKeyTask => {
const path = [...basePath, key];
const validator = compiler<unknown>(schema[key as keyof S], {
basePath: path,
basePath,
strict,
});

Expand Down

0 comments on commit faac9ee

Please sign in to comment.