Skip to content

Commit 39e61e2

Browse files
committed
✨ feat: add error message
1 parent e4459e0 commit 39e61e2

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Diff for: __test__/locale.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Language } from '../utils/getLanguage'
55
import { includeAllKeys, excludeKeys } from './utils'
66

77
const locales = readdirSync(resolve(__dirname, '../locales')).filter((file) => {
8-
return file.includes('.json') // exclude unnecessary files
8+
return file.includes('.json')
99
})
1010

1111
describe('should match name regex', () => {
@@ -17,6 +17,7 @@ describe('should match name regex', () => {
1717
* @example reusable locale: zh-Hant
1818
*/
1919
const regex = /^[a-zA-Z]{2}(-[a-zA-Z]{2})*.json$|^[a-zA-Z]{2}(-[a-zA-z]{4})*.json$/
20+
2021
locales.forEach((locale) => {
2122
it(`for ${locale}`, () => {
2223
expect(locale).toMatch(regex)
@@ -33,7 +34,7 @@ describe('should include full keys', () => {
3334
})
3435
})
3536

36-
describe("should not include unnecessary keys", () => {
37+
describe("should not include extra keys", () => {
3738
const structure = require('../schema/locale.json') as Language
3839
locales.forEach((locale) => {
3940
it(`for ${locale}`, () => {

Diff for: __test__/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
export function includeAllKeys(obj: Object, schema: Object) {
88
for (let key in schema) {
99
if (!obj.hasOwnProperty(key)) {
10+
console.log(`key '${key}' lost`)
1011
return false
1112
}
1213
if (schema[key] !== null) {
1314
if (typeof schema[key] === 'string') {
1415
if (typeof obj[key] !== schema[key]) {
16+
console.error(`the type of ${obj.toString()} is incorrect`)
1517
return false
1618
}
1719
} else if (typeof schema[key] === 'object') {
@@ -33,6 +35,7 @@ export function includeAllKeys(obj: Object, schema: Object) {
3335
export function excludeKeys(obj: Object, schema: Object) {
3436
for (let key in obj) {
3537
if (!schema.hasOwnProperty(key)) {
38+
console.error(`unexpected key: ${key}`)
3639
return false
3740
}
3841
if (schema[key] !== null && typeof schema[key] === 'object') {

0 commit comments

Comments
 (0)