ConstNote that most iterators are also iterable (they have Symbol.iterator
returning this), but this schema specifically checks for the next()
method which is the core iterator requirement.
const arr = [1, 2, 3];
SyncIteratorSchema.parse(arr[Symbol.iterator]()); // ✓ Valid
SyncIteratorSchema.parse(
(function* () {
yield 1;
})(),
); // ✓ Valid (generators are iterators)
SyncIteratorSchema.parse({
next: () => ({ done: true, value: undefined }),
}); // ✓ Valid
SyncIteratorSchema.parse([1, 2, 3]); // ✗ Throws (array is iterable, not iterator)
SyncIteratorSchema.parse({}); // ✗ Throws validation error
import { createAssertion, use } from 'bupkis';
import { SyncIteratorSchema } from 'bupkis/schema';
const iteratorAssertion = createAssertion(
[SyncIteratorSchema, 'to be an iterator'],
SyncIteratorSchema,
);
const { expect } = use([iteratorAssertion]);
expect([1, 2, 3][Symbol.iterator](), 'to be an iterator');
Schema matching any synchronous iterator (has
next()method).This schema validates values that implement the iterator protocol, meaning they have a
next()method that returns{ value, done }objects. This includes iterator objects returned from callingSymbol.iteratoron iterables, generator objects, and custom iterator implementations.