Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@sidebase/nuxt-parse
Advanced tools
Readme
A nuxt focused package to make data validation and parsing easy. This package follows the design philosophy of the article parse, don't validate. It uses zod
for parsing data from the user, APIs, your own functions, ...
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
Moved here from original mono-repo
zod
npm i @sidebase/nuxt-parse
Then, e.g., in your code:
import { z, makeParser } from "@sidebase/nuxt-parse"
// Define the expected response schema
const responseSchema = z.object({
uuid: z.string().uuid(),
})
// Perform the request, use `makeParse` to pass a transformer for the data
const { data, error } = await useFetch('https://httpbin.org/uuid', {
transform: makeParser(responseSchema),
})
console.log(`data is ${data.value}`)
// -> `data is {"uuid":"f8df921c-d7f3-43c1-ac9b-3cf5d4da2f7b"}`
console.log(`error is ${error.value}`)
// -> `error is false`
import { z, makeParser } from "@sidebase/nuxt-parse"
// Define the expected response schema
const responseSchema = z.object({
uuid: z.string().uuid(),
})
// Perform the request, use `makeParse` to pass a transformer for the data
const { data, error } = await useFetch('https://httpbin.org/ip', {
transform: makeParser(responseSchema),
})
console.log(`data is ${data.value}`)
// -> `data is null`
console.log(`error is ${error.value}`)
// -> `error is true`
import { defineEventHandler } from 'h3'
import type { CompatibilityEvent } from 'h3'
import { z, parseParamsAs, parseBodyAs } from "@sidebase/nuxt-parse"
// Define the schema of the parameters you expect the user to provide you with
const paramsSchema = z.object({
id: z.string().uuid(),
})
// Define the schema of the body you expect the user to provide you with
const bodySchema = z.object({
name: z.string(),
age: z.number()
})
// Get a nice type to use throughout your code and components
type RequestBody = z.infer<typeof bodySchema>
export default defineEventHandler(async (event: CompatibilityEvent) => {
// Validate and then get the parameters
// This automatically throws a nice HTTP 422 error with more information if the data is invalid
const params = parseParamsAs(event, paramsSchema)
let body: RequestBody;
try {
body = parseBodyAs(event, paramsSchema)
} catch(error) {
// Fallback, this avoids automatic raising + returning of the HTTP 422 error
body = {
name: 'Bernd',
age: 88
}
}
// Return the full entity
return {
id: params.id,
...body
}
})
import { z, parseDataAs } from "@sidebase/nuxt-parse"
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.number() )}))
// -> throws! `"1"` is not a number, but a string!
const parsedData = await parseDataAs({ test: 1 }, z.object({ test: z.number() )}))
console.log(parsedData)
// -> output: `{ test: 1 }`
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.string().transform(v => parseInt(v)) )}))
console.log(parsedData)
// -> output: `{ test: 1 }` (we used `.transform` to ensure that we get a number)
import { z, parseDataAs } from "@sidebase/nuxt-parse"
const fakeDatabaseQuery = async () => { id: 1 }
const parsedData = await parseDataAs(fakeDatabaseQuery, z.object({ id: z.number() )}))
console.log(parsedData)
// -> output: `1`
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
This module exports:
parseBodyAs
: Parse body of h3
eventparseParamsAs
: Parse params of h3
eventparseQueryAs
: Parse query of h3
eventparseCookieAs
: Parse cookies of h3
eventparseHeaderAs
: Parse header of h3
eventparseDataAs
: Parse sync or async datamakeParser
: Make your own parser (see example above)z
: zod
, the library used for parsingnpm run test
to generate type stubsnpm run lint
to run eslintnpm run type
to run typescheck via tscnpm publish
to run build and publish the packageFAQs
Parse, validate and transform data with confidence in nuxt using `zod`
The npm package @sidebase/nuxt-parse receives a total of 208 weekly downloads. As such, @sidebase/nuxt-parse popularity was classified as not popular.
We found that @sidebase/nuxt-parse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.