Skip to content

Commit

Permalink
Merge pull request #2023 from openzim/count-graphemes
Browse files Browse the repository at this point in the history
Measure Metadata length by grapheme
  • Loading branch information
kelson42 authored May 7, 2024
2 parents dddf344 + d82f127 commit 9cc613f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
16 changes: 11 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"rimraf": "^4.4.1",
"semver": "^7.3.8",
"sharp": "^0.32.6",
"split-by-grapheme": "^1.0.1",
"swig-templates": "^2.0.3",
"typescript": "^4.9.4",
"utf8-binary-cutter": "^0.9.2",
Expand Down
21 changes: 18 additions & 3 deletions src/util/metaData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AjvModule from 'ajv'
import type { KeywordCxt } from 'ajv'
import { byGrapheme } from 'split-by-grapheme'

const Ajv = AjvModule.default
const ajv = new Ajv({ allErrors: true })
Expand All @@ -18,18 +20,31 @@ ajv.addKeyword({
},
})

ajv.addKeyword({
keyword: 'uMaxLength',
type: 'string',
validate: (max_length: number, value) => {
return value.split(byGrapheme).length <= max_length
},
error: {
message: (cxt: KeywordCxt): string => {
return `must NOT have more than ${cxt.schemaValue} graphemes`
},
},
})

const schema = {
type: 'object',
properties: {
Name: { type: 'string', minLength: 1 },
Creator: { type: 'string', minLength: 1 },
Description: { type: 'string', maxLength: 80, minLength: 1 },
Description: { type: 'string', uMaxLength: 80, minLength: 1 },
Language: { type: 'string', minLength: 1, pattern: '^\\w{3}(,\\w{3})*$' },
Publisher: { type: 'string', minLength: 1 },
Title: { type: 'string', maxLength: 30, minLength: 1 },
Title: { type: 'string', uMaxLength: 30, minLength: 1 },
Date: { type: 'string', maxLength: 10, minLength: 10 },
'Illustration_48x48@1': { checkRegexFromBuffer: '^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a.+' },
LongDescription: { type: 'string', maxLength: 4000 },
LongDescription: { type: 'string', uMaxLength: 4000 },
License: { type: 'string' },
Tags: { type: 'string' },
Relation: { type: 'string' },
Expand Down
8 changes: 4 additions & 4 deletions test/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ describe('Utils', () => {
Description: 'test Description',
Language: 'eng,ita',
Publisher: 'test Publisher',
Title: 'test Title',
Title: 'विकी मेड मेडिकल इनसाइक्लोपीडिया हिंदी मेंfविविविमे',
'Illustration_48x48@1': pngImage,
}

Expand Down Expand Up @@ -459,15 +459,15 @@ describe('Utils', () => {
...minimumValidMetadata,
Description: 'test Description test Description test Description test Description test Description test Description ',
}
expect(() => validateMetadata(metaData)).toThrow('MetaData Description: must NOT have more than 80 characters')
expect(() => validateMetadata(metaData)).toThrow('MetaData Description: must NOT have more than 80 graphemes')
})

test('validate long Title', () => {
const metaData = {
...minimumValidMetadata,
Title: 'test Title test Title test Title',
Title: 'विकी मेड मेडिकल इनसाइक्लोपीडिया हिंदी मेंfविविविमेविमेवि',
}
expect(() => validateMetadata(metaData)).toThrow('MetaData Title: must NOT have more than 30 characters')
expect(() => validateMetadata(metaData)).toThrow('MetaData Title: must NOT have more than 30 graphemes')
})

test('validate string with line brake', () => {
Expand Down

0 comments on commit 9cc613f

Please sign in to comment.