or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mdbuiltin-types.mdcollection-types.mdindex.mdobject-array-validation.mdprimitive-types.mdtyped-arrays.md

typed-arrays.mddocs/

0

# Typed Array Validation

1

2

Comprehensive validation for all typed array types including length constraints, byte length validation, and binary data validation methods.

3

4

## Capabilities

5

6

### Generic Typed Array Validation

7

8

Base validation for any typed array with common length and byte length methods.

9

10

```typescript { .api }

11

/** Generic typed array validation predicate */

12

interface TypedArrayPredicate<T extends TypedArray> extends BasePredicate<T> {

13

// Length validation (number of elements)

14

length(length: number): TypedArrayPredicate<T>;

15

minLength(length: number): TypedArrayPredicate<T>;

16

maxLength(length: number): TypedArrayPredicate<T>;

17

18

// Byte length validation (total bytes)

19

byteLength(byteLength: number): TypedArrayPredicate<T>;

20

minByteLength(byteLength: number): TypedArrayPredicate<T>;

21

maxByteLength(byteLength: number): TypedArrayPredicate<T>;

22

}

23

24

/** Base typed array type */

25

type TypedArray =

26

| Int8Array

27

| Uint8Array

28

| Uint8ClampedArray

29

| Int16Array

30

| Uint16Array

31

| Int32Array

32

| Uint32Array

33

| Float32Array

34

| Float64Array;

35

```

36

37

### 8-bit Typed Arrays

38

39

Validation for 8-bit typed arrays with 1 byte per element.

40

41

```typescript { .api }

42

/** 8-bit signed integer array (-128 to 127) */

43

const int8Array: TypedArrayPredicate<Int8Array>;

44

45

/** 8-bit unsigned integer array (0 to 255) */

46

const uint8Array: TypedArrayPredicate<Uint8Array>;

47

48

/** 8-bit unsigned integer clamped array (0 to 255, clamped) */

49

const uint8ClampedArray: TypedArrayPredicate<Uint8ClampedArray>;

50

```

51

52

**Usage Examples:**

53

54

```typescript

55

import ow from 'ow';

56

57

// Int8Array validation

58

const int8 = new Int8Array([127, -128, 0]);

59

ow(int8, ow.int8Array);

60

ow(int8, ow.int8Array.length(3));

61

ow(int8, ow.int8Array.byteLength(3)); // 3 elements × 1 byte = 3 bytes

62

63

// Uint8Array validation

64

const uint8 = new Uint8Array([255, 0, 128]);

65

ow(uint8, ow.uint8Array);

66

ow(uint8, ow.uint8Array.minLength(2));

67

ow(uint8, ow.uint8Array.maxByteLength(10));

68

69

// Uint8ClampedArray validation (used in Canvas ImageData)

70

const clamped = new Uint8ClampedArray([300, -50, 128]); // Values clamped to 0-255

71

ow(clamped, ow.uint8ClampedArray);

72

ow(clamped, ow.uint8ClampedArray.length(3));

73

```

74

75

### 16-bit Typed Arrays

76

77

Validation for 16-bit typed arrays with 2 bytes per element.

78

79

```typescript { .api }

80

/** 16-bit signed integer array (-32768 to 32767) */

81

const int16Array: TypedArrayPredicate<Int16Array>;

82

83

/** 16-bit unsigned integer array (0 to 65535) */

84

const uint16Array: TypedArrayPredicate<Uint16Array>;

85

```

86

87

**Usage Examples:**

88

89

```typescript

90

import ow from 'ow';

91

92

// Int16Array validation

93

const int16 = new Int16Array([32767, -32768, 0]);

94

ow(int16, ow.int16Array);

95

ow(int16, ow.int16Array.length(3));

96

ow(int16, ow.int16Array.byteLength(6)); // 3 elements × 2 bytes = 6 bytes

97

98

// Uint16Array validation

99

const uint16 = new Uint16Array([65535, 0, 32768]);

100

ow(uint16, ow.uint16Array);

101

ow(uint16, ow.uint16Array.minLength(2));

102

ow(uint16, ow.uint16Array.maxByteLength(20));

103

```

104

105

### 32-bit Typed Arrays

106

107

Validation for 32-bit typed arrays with 4 bytes per element.

108

109

```typescript { .api }

110

/** 32-bit signed integer array (-2147483648 to 2147483647) */

111

const int32Array: TypedArrayPredicate<Int32Array>;

112

113

/** 32-bit unsigned integer array (0 to 4294967295) */

114

const uint32Array: TypedArrayPredicate<Uint32Array>;

115

116

/** 32-bit floating point array */

117

const float32Array: TypedArrayPredicate<Float32Array>;

118

```

119

120

**Usage Examples:**

121

122

```typescript

123

import ow from 'ow';

124

125

// Int32Array validation

126

const int32 = new Int32Array([2147483647, -2147483648, 0]);

127

ow(int32, ow.int32Array);

128

ow(int32, ow.int32Array.length(3));

129

ow(int32, ow.int32Array.byteLength(12)); // 3 elements × 4 bytes = 12 bytes

130

131

// Uint32Array validation

132

const uint32 = new Uint32Array([4294967295, 0, 2147483648]);

133

ow(uint32, ow.uint32Array);

134

ow(uint32, ow.uint32Array.minLength(1));

135

136

// Float32Array validation

137

const float32 = new Float32Array([3.14159, -2.71828, 0.0]);

138

ow(float32, ow.float32Array);

139

ow(float32, ow.float32Array.maxLength(10));

140

ow(float32, ow.float32Array.byteLength(12)); // 3 elements × 4 bytes = 12 bytes

141

```

142

143

### 64-bit Typed Arrays

144

145

Validation for 64-bit floating point arrays with 8 bytes per element.

146

147

```typescript { .api }

148

/** 64-bit floating point array */

149

const float64Array: TypedArrayPredicate<Float64Array>;

150

```

151

152

**Usage Examples:**

153

154

```typescript

155

import ow from 'ow';

156

157

// Float64Array validation

158

const float64 = new Float64Array([Math.PI, Math.E, Number.MAX_VALUE]);

159

ow(float64, ow.float64Array);

160

ow(float64, ow.float64Array.length(3));

161

ow(float64, ow.float64Array.byteLength(24)); // 3 elements × 8 bytes = 24 bytes

162

163

// High precision validation

164

ow(float64, ow.float64Array.minByteLength(8));

165

ow(float64, ow.float64Array.maxByteLength(1024));

166

```

167

168

### Generic Typed Array Validation

169

170

Validation for any typed array when the specific type is not known.

171

172

```typescript { .api }

173

/** Generic typed array validation */

174

const typedArray: TypedArrayPredicate<TypedArray>;

175

```

176

177

**Usage Examples:**

178

179

```typescript

180

import ow from 'ow';

181

182

// Generic typed array validation

183

ow(new Int8Array([1, 2, 3]), ow.typedArray);

184

ow(new Float32Array([1.1, 2.2, 3.3]), ow.typedArray);

185

ow(new Uint16Array([100, 200, 300]), ow.typedArray);

186

187

// When you don't know the specific type

188

function processTypedArray(data: unknown) {

189

ow(data, ow.typedArray.minLength(1));

190

191

const typedData = data as TypedArray;

192

console.log(`Array type: ${typedData.constructor.name}`);

193

console.log(`Length: ${typedData.length}`);

194

console.log(`Byte length: ${typedData.byteLength}`);

195

}

196

```

197

198

## ArrayBuffer and DataView Validation

199

200

### ArrayBuffer Validation

201

202

Validation for ArrayBuffer objects with byte length constraints.

203

204

```typescript { .api }

205

/** ArrayBuffer validation predicate */

206

interface ArrayBufferPredicate<T extends ArrayBuffer> extends BasePredicate<T> {

207

byteLength(byteLength: number): ArrayBufferPredicate<T>;

208

minByteLength(byteLength: number): ArrayBufferPredicate<T>;

209

maxByteLength(byteLength: number): ArrayBufferPredicate<T>;

210

}

211

212

/** Standard ArrayBuffer validation */

213

const arrayBuffer: ArrayBufferPredicate<ArrayBuffer>;

214

215

/** SharedArrayBuffer validation */

216

const sharedArrayBuffer: ArrayBufferPredicate<SharedArrayBuffer>;

217

```

218

219

**Usage Examples:**

220

221

```typescript

222

import ow from 'ow';

223

224

// ArrayBuffer validation

225

const buffer = new ArrayBuffer(16);

226

ow(buffer, ow.arrayBuffer);

227

ow(buffer, ow.arrayBuffer.byteLength(16));

228

ow(buffer, ow.arrayBuffer.minByteLength(8));

229

ow(buffer, ow.arrayBuffer.maxByteLength(32));

230

231

// SharedArrayBuffer validation (if supported)

232

if (typeof SharedArrayBuffer !== 'undefined') {

233

const sharedBuffer = new SharedArrayBuffer(32);

234

ow(sharedBuffer, ow.sharedArrayBuffer);

235

ow(sharedBuffer, ow.sharedArrayBuffer.byteLength(32));

236

}

237

```

238

239

### DataView Validation

240

241

Validation for DataView objects with byte length constraints.

242

243

```typescript { .api }

244

/** DataView validation predicate */

245

interface DataViewPredicate extends BasePredicate<DataView> {

246

byteLength(byteLength: number): DataViewPredicate;

247

minByteLength(byteLength: number): DataViewPredicate;

248

maxByteLength(byteLength: number): DataViewPredicate;

249

}

250

251

const dataView: DataViewPredicate;

252

```

253

254

**Usage Examples:**

255

256

```typescript

257

import ow from 'ow';

258

259

// DataView validation

260

const buffer = new ArrayBuffer(16);

261

const view = new DataView(buffer, 4, 8); // offset 4, length 8

262

263

ow(view, ow.dataView);

264

ow(view, ow.dataView.byteLength(8));

265

ow(view, ow.dataView.minByteLength(4));

266

ow(view, ow.dataView.maxByteLength(16));

267

```

268

269

## Advanced Usage Examples

270

271

### Binary Data Processing

272

273

```typescript

274

import ow from 'ow';

275

276

// Validate binary data processing pipeline

277

function processBinaryData(input: unknown): Uint8Array {

278

// Validate input can be converted to Uint8Array

279

if (ow.isValid(input, ow.arrayBuffer)) {

280

const buffer = input as ArrayBuffer;

281

ow(buffer, ow.arrayBuffer.minByteLength(1));

282

return new Uint8Array(buffer);

283

}

284

285

if (ow.isValid(input, ow.typedArray)) {

286

const typedArray = input as TypedArray;

287

ow(typedArray, ow.typedArray.minLength(1));

288

return new Uint8Array(typedArray.buffer);

289

}

290

291

throw new Error('Input must be ArrayBuffer or TypedArray');

292

}

293

294

// Usage

295

const buffer = new ArrayBuffer(10);

296

const result = processBinaryData(buffer);

297

ow(result, ow.uint8Array.length(10));

298

```

299

300

### Image Data Validation

301

302

```typescript

303

import ow from 'ow';

304

305

// Validate image data (Canvas ImageData-like structure)

306

interface ImageData {

307

data: Uint8ClampedArray;

308

width: number;

309

height: number;

310

}

311

312

function validateImageData(imageData: unknown) {

313

ow(imageData, ow.object.exactShape({

314

data: ow.uint8ClampedArray,

315

width: ow.number.integer.positive,

316

height: ow.number.integer.positive

317

}));

318

319

const { data, width, height } = imageData as ImageData;

320

321

// RGBA data should have exactly width * height * 4 bytes

322

const expectedLength = width * height * 4;

323

ow(data, ow.uint8ClampedArray.length(expectedLength));

324

ow(data, ow.uint8ClampedArray.byteLength(expectedLength));

325

}

326

327

// Usage

328

const imageData = {

329

data: new Uint8ClampedArray(400), // 10x10 RGBA

330

width: 10,

331

height: 10

332

};

333

334

validateImageData(imageData);

335

```

336

337

### Audio Buffer Validation

338

339

```typescript

340

import ow from 'ow';

341

342

// Validate audio buffer data

343

interface AudioBuffer {

344

sampleRate: number;

345

length: number;

346

numberOfChannels: number;

347

channels: Float32Array[];

348

}

349

350

function validateAudioBuffer(audioBuffer: unknown) {

351

ow(audioBuffer, ow.object.exactShape({

352

sampleRate: ow.number.positive,

353

length: ow.number.integer.positive,

354

numberOfChannels: ow.number.integer.inRange(1, 32),

355

channels: ow.array.ofType(ow.float32Array)

356

}));

357

358

const { channels, length, numberOfChannels } = audioBuffer as AudioBuffer;

359

360

// Validate channel count matches array length

361

ow(channels, ow.array.length(numberOfChannels));

362

363

// Validate each channel has correct length

364

channels.forEach((channel, index) => {

365

ow(channel, `channel${index}`, ow.float32Array.length(length));

366

});

367

}

368

```

369

370

### Memory-Mapped Data Validation

371

372

```typescript

373

import ow from 'ow';

374

375

// Validate memory-mapped data structures

376

class MemoryMappedStruct {

377

private view: DataView;

378

379

constructor(buffer: ArrayBuffer, offset = 0) {

380

ow(buffer, ow.arrayBuffer.minByteLength(offset + 32));

381

this.view = new DataView(buffer, offset, 32);

382

ow(this.view, ow.dataView.byteLength(32));

383

}

384

385

getInt32(offset: number): number {

386

ow(offset, ow.number.integer.inRange(0, 28)); // 32 - 4 bytes

387

return this.view.getInt32(offset, true); // little-endian

388

}

389

390

setFloat64(offset: number, value: number): void {

391

ow(offset, ow.number.integer.inRange(0, 24)); // 32 - 8 bytes

392

ow(value, ow.number.finite);

393

this.view.setFloat64(offset, value, true);

394

}

395

}

396

397

// Usage

398

const buffer = new ArrayBuffer(64);

399

const struct = new MemoryMappedStruct(buffer, 0);

400

struct.setFloat64(0, 3.14159);

401

```