@atproto/lexicon
Advanced tools
Comparing version 0.4.1 to 0.4.2
# @atproto/lexicon | ||
## 0.4.2 | ||
### Patch Changes | ||
- [#2817](https://github.com/bluesky-social/atproto/pull/2817) [`87a1f2426`](https://github.com/bluesky-social/atproto/commit/87a1f24262e0e644b6cf31cc7a0446d9127ffa94) Thanks [@gaearon](https://github.com/gaearon)! - Add fast path skipping grapheme counting | ||
- Updated dependencies [[`a07b21151`](https://github.com/bluesky-social/atproto/commit/a07b21151f1850340c4b7797ebb11521b1a6cdf3), [`a07b21151`](https://github.com/bluesky-social/atproto/commit/a07b21151f1850340c4b7797ebb11521b1a6cdf3), [`eb20ff64a`](https://github.com/bluesky-social/atproto/commit/eb20ff64a2d8e3061c652e1e247bf9b0fe3c41a6)]: | ||
- @atproto/common-web@0.3.1 | ||
## 0.4.1 | ||
@@ -4,0 +13,0 @@ |
@@ -178,37 +178,70 @@ "use strict"; | ||
} | ||
// maxLength | ||
if (typeof def.maxLength === 'number') { | ||
if ((0, common_web_1.utf8Len)(value) > def.maxLength) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be longer than ${def.maxLength} characters`), | ||
}; | ||
// maxLength and minLength | ||
if (typeof def.maxLength === 'number' || typeof def.minLength === 'number') { | ||
const len = (0, common_web_1.utf8Len)(value); | ||
if (typeof def.maxLength === 'number') { | ||
if (len > def.maxLength) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be longer than ${def.maxLength} characters`), | ||
}; | ||
} | ||
} | ||
} | ||
// minLength | ||
if (typeof def.minLength === 'number') { | ||
if ((0, common_web_1.utf8Len)(value) < def.minLength) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be shorter than ${def.minLength} characters`), | ||
}; | ||
if (typeof def.minLength === 'number') { | ||
if (len < def.minLength) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be shorter than ${def.minLength} characters`), | ||
}; | ||
} | ||
} | ||
} | ||
// maxGraphemes | ||
if (typeof def.maxGraphemes === 'number') { | ||
if ((0, common_web_1.graphemeLen)(value) > def.maxGraphemes) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be longer than ${def.maxGraphemes} graphemes`), | ||
}; | ||
// maxGraphemes and minGraphemes | ||
if (typeof def.maxGraphemes === 'number' || | ||
typeof def.minGraphemes === 'number') { | ||
let needsMaxGraphemesCheck = false; | ||
let needsMinGraphemesCheck = false; | ||
if (typeof def.maxGraphemes === 'number') { | ||
if (value.length <= def.maxGraphemes) { | ||
// If the JavaScript string length (UTF-16) is within the maximum limit, | ||
// its grapheme length (which <= .length) will also be within. | ||
needsMaxGraphemesCheck = false; | ||
} | ||
else { | ||
needsMaxGraphemesCheck = true; | ||
} | ||
} | ||
} | ||
// minGraphemes | ||
if (typeof def.minGraphemes === 'number') { | ||
if ((0, common_web_1.graphemeLen)(value) < def.minGraphemes) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be shorter than ${def.minGraphemes} graphemes`), | ||
}; | ||
if (typeof def.minGraphemes === 'number') { | ||
if (value.length < def.minGraphemes) { | ||
// If the JavaScript string length (UTF-16) is below the minimal limit, | ||
// its grapheme length (which <= .length) will also be below. | ||
// Fail early. | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be shorter than ${def.minGraphemes} graphemes`), | ||
}; | ||
} | ||
else { | ||
needsMinGraphemesCheck = true; | ||
} | ||
} | ||
if (needsMaxGraphemesCheck || needsMinGraphemesCheck) { | ||
const len = (0, common_web_1.graphemeLen)(value); | ||
if (typeof def.maxGraphemes === 'number') { | ||
if (len > def.maxGraphemes) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be longer than ${def.maxGraphemes} graphemes`), | ||
}; | ||
} | ||
} | ||
if (typeof def.minGraphemes === 'number') { | ||
if (len < def.minGraphemes) { | ||
return { | ||
success: false, | ||
error: new types_1.ValidationError(`${path} must not be shorter than ${def.minGraphemes} graphemes`), | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
@@ -215,0 +248,0 @@ if (typeof def.format === 'string') { |
{ | ||
"name": "@atproto/lexicon", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"license": "MIT", | ||
@@ -22,3 +22,3 @@ "description": "atproto Lexicon schema language library", | ||
"zod": "^3.23.8", | ||
"@atproto/common-web": "^0.3.0", | ||
"@atproto/common-web": "^0.3.1", | ||
"@atproto/syntax": "^0.3.0" | ||
@@ -25,0 +25,0 @@ }, |
@@ -200,22 +200,25 @@ import { utf8Len, graphemeLen } from '@atproto/common-web' | ||
// maxLength | ||
if (typeof def.maxLength === 'number') { | ||
if (utf8Len(value) > def.maxLength) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be longer than ${def.maxLength} characters`, | ||
), | ||
// maxLength and minLength | ||
if (typeof def.maxLength === 'number' || typeof def.minLength === 'number') { | ||
const len = utf8Len(value) | ||
if (typeof def.maxLength === 'number') { | ||
if (len > def.maxLength) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be longer than ${def.maxLength} characters`, | ||
), | ||
} | ||
} | ||
} | ||
} | ||
// minLength | ||
if (typeof def.minLength === 'number') { | ||
if (utf8Len(value) < def.minLength) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be shorter than ${def.minLength} characters`, | ||
), | ||
if (typeof def.minLength === 'number') { | ||
if (len < def.minLength) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be shorter than ${def.minLength} characters`, | ||
), | ||
} | ||
} | ||
@@ -225,24 +228,61 @@ } | ||
// maxGraphemes | ||
if (typeof def.maxGraphemes === 'number') { | ||
if (graphemeLen(value) > def.maxGraphemes) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be longer than ${def.maxGraphemes} graphemes`, | ||
), | ||
// maxGraphemes and minGraphemes | ||
if ( | ||
typeof def.maxGraphemes === 'number' || | ||
typeof def.minGraphemes === 'number' | ||
) { | ||
let needsMaxGraphemesCheck = false | ||
let needsMinGraphemesCheck = false | ||
if (typeof def.maxGraphemes === 'number') { | ||
if (value.length <= def.maxGraphemes) { | ||
// If the JavaScript string length (UTF-16) is within the maximum limit, | ||
// its grapheme length (which <= .length) will also be within. | ||
needsMaxGraphemesCheck = false | ||
} else { | ||
needsMaxGraphemesCheck = true | ||
} | ||
} | ||
} | ||
// minGraphemes | ||
if (typeof def.minGraphemes === 'number') { | ||
if (graphemeLen(value) < def.minGraphemes) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be shorter than ${def.minGraphemes} graphemes`, | ||
), | ||
if (typeof def.minGraphemes === 'number') { | ||
if (value.length < def.minGraphemes) { | ||
// If the JavaScript string length (UTF-16) is below the minimal limit, | ||
// its grapheme length (which <= .length) will also be below. | ||
// Fail early. | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be shorter than ${def.minGraphemes} graphemes`, | ||
), | ||
} | ||
} else { | ||
needsMinGraphemesCheck = true | ||
} | ||
} | ||
if (needsMaxGraphemesCheck || needsMinGraphemesCheck) { | ||
const len = graphemeLen(value) | ||
if (typeof def.maxGraphemes === 'number') { | ||
if (len > def.maxGraphemes) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be longer than ${def.maxGraphemes} graphemes`, | ||
), | ||
} | ||
} | ||
} | ||
if (typeof def.minGraphemes === 'number') { | ||
if (len < def.minGraphemes) { | ||
return { | ||
success: false, | ||
error: new ValidationError( | ||
`${path} must not be shorter than ${def.minGraphemes} graphemes`, | ||
), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -249,0 +289,0 @@ |
@@ -595,18 +595,95 @@ import { CID } from 'multiformats/cid' | ||
it('Applies grapheme string length constraint', () => { | ||
// Shorter than two graphemes | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: '', | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: '\u0301\u0301\u0301', // Three combining acute accents | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'a', | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'a\u0301\u0301\u0301\u0301', // 'aΜΜΜΜ' ('a' with four combining acute accents) | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: '5\uFE0F', // '5οΈ' with emoji presentation | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'π¨βπ©βπ§βπ§', | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
// Two to four graphemes | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'ab', | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'a\u0301b', // 'aΜb' with combining accent | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'a\u0301b\u0301', // 'aΜbΜ' | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'ππ', | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: '12π¨βπ©βπ§βπ§', | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'abcd', | ||
}) | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'a\u0301b\u0301c\u0301d\u0301', // 'aΜbΜcΜdΜ' | ||
}) | ||
// Longer than four graphemes | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'π¨βπ©βπ§βπ§', | ||
string: 'abcde', | ||
}), | ||
).toThrow('Record/string must not be shorter than 2 graphemes') | ||
).toThrow('Record/string must not be longer than 4 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: '12345', | ||
string: 'a\u0301b\u0301c\u0301d\u0301e\u0301', // 'aΜbΜcΜdΜeΜ' | ||
}), | ||
).toThrow('Record/string must not be longer than 4 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'πππππ', | ||
}), | ||
).toThrow('Record/string must not be longer than 4 graphemes') | ||
expect(() => | ||
lex.assertValidRecord('com.example.stringLengthGrapheme', { | ||
$type: 'com.example.stringLengthGrapheme', | ||
string: 'abπde', | ||
}), | ||
).toThrow('Record/string must not be longer than 4 graphemes') | ||
}) | ||
@@ -613,0 +690,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1174941
27583
Updated@atproto/common-web@^0.3.1