Skip to content

Commit e2ff91c

Browse files
committed
Add skipValidation option to .load()
1 parent 5e20d86 commit e2ff91c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Diff for: README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ chess.isThreefoldRepetition()
527527
// -> true
528528
```
529529

530-
### .load(fen: string, { preserveHeaders = false } = {})
530+
### .load(fen: string, { skipValidation = false, preserveHeaders = false } = {})
531531

532532
Clears the board and loads the provided FEN string. The castling rights, en
533533
passant square and move numbers are defaulted to `- - 0 1` if omitted. Throws an
@@ -538,11 +538,14 @@ const chess = new Chess()
538538
chess.load('4r3/8/2p2PPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45')
539539

540540
try {
541-
chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45')
541+
chess.load('8/4p3/8/8/8/8/4P3/6K1 w - - 1 45')
542542
} catch (e) {
543543
console.log(e)
544544
}
545-
// -> Error: Invalid FEN: piece data is invalid (invalid piece)
545+
// -> Error: Invalid FEN: missing black king
546+
547+
chess.load('8/4p3/8/8/8/8/4P3/6K1 w - - 1 45', { skipValidation: true })
548+
// -> Works!
546549
```
547550

548551
### .loadPgn(pgn, [ options ])

Diff for: __tests__/load.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ test('load - bad piece (X)', () => {
3131
expect(() => chess.load(fen)).toThrow()
3232
})
3333

34+
test('load - missing white king', () => {
35+
const chess = new Chess()
36+
const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
37+
expect(() => chess.load(fen)).toThrow()
38+
})
39+
40+
test('load - missing black king', () => {
41+
const chess = new Chess()
42+
const fen = '1nbq1bn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQKBN1 b - - 1 2'
43+
expect(() => chess.load(fen)).toThrow()
44+
})
45+
3446
test('load - bad ep square (e9)', () => {
3547
const chess = new Chess()
3648
const fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e9 0 1'
@@ -61,3 +73,10 @@ test('load - preserveHeaders = true', () => {
6173
Black: 'Viswanathan Anand',
6274
})
6375
})
76+
77+
test('load - skipValidation = true', () => {
78+
const chess = new Chess()
79+
// missing white king
80+
const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
81+
expect(() => chess.load(fen, { skipValidation: true })).not.toThrow()
82+
})

Diff for: src/chess.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ export class Chess {
598598
}
599599
}
600600

601-
load(fen: string, { preserveHeaders = false } = {}) {
601+
load(fen: string, { skipValidation = false, preserveHeaders = false } = {}) {
602602
let tokens = fen.split(/\s+/)
603603

604604
// append commonly omitted fen tokens
@@ -609,9 +609,11 @@ export class Chess {
609609

610610
tokens = fen.split(/\s+/)
611611

612-
const { ok, error } = validateFen(fen)
613-
if (!ok) {
614-
throw new Error(error)
612+
if (!skipValidation) {
613+
const { ok, error } = validateFen(fen)
614+
if (!ok) {
615+
throw new Error(error)
616+
}
615617
}
616618

617619
const position = tokens[0]

0 commit comments

Comments
 (0)